jquery 表单标签?它们不存在吗?

发布于 2024-12-08 17:07:18 字数 727 浏览 0 评论 0原文

我正在创建一个动态输入元素,我想为其设置标签...我已经尝试了很多方法...

//get the value from the input
var getValue = $('#newField').val(); 

//create new input and set default value
var newField = document.createElement("input");
    newField.setAttribute("type", "text");
    newField.setAttribute("name", "s[x]np");
    newField.setAttribute("id", "sx");
    newField.setAttribute("class", "st form-text");
    newField.setAttribute("value", getValue);

$('#addithere').append(newField);

//I've tried all of the following:
newField.prepend("Label");
...

newField.before("Label");
...

$('#sx').prepend("Label");
...

$('#sx').before("Label");
...

var secondItem = "Section: "+newField;
$('#addithere').append(secondItem);

I'm creating a dynamic input element and I want to set the label for it... I've tried a number of things...

//get the value from the input
var getValue = $('#newField').val(); 

//create new input and set default value
var newField = document.createElement("input");
    newField.setAttribute("type", "text");
    newField.setAttribute("name", "s[x]np");
    newField.setAttribute("id", "sx");
    newField.setAttribute("class", "st form-text");
    newField.setAttribute("value", getValue);

$('#addithere').append(newField);

//I've tried all of the following:
newField.prepend("Label");
...

newField.before("Label");
...

$('#sx').prepend("Label");
...

$('#sx').before("Label");
...

var secondItem = "Section: "+newField;
$('#addithere').append(secondItem);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼窥荷 2024-12-15 17:07:18

$('#sx').prepend("标签");

这不是创建新标签元素的正确语法。你应该使用这样的东西:

$('#sx').prepend($('<label/>'));

$('#sx').prepend("Label");

This is not the correct syntax for creating a new label element. You should be using something like this:

$('#sx').prepend($('<label/>'));
诠释孤独 2024-12-15 17:07:18

为什么不尝试 $(newField).before('');


编辑:
也许您没有 $(document).ready(function(){....});
我尝试了你的代码,没有这个 - 它不起作用......但是有了它,它起作用了!

$(document).ready(function(){
var getValue = $('#newField').val();
var newField = document.createElement("input");
newField.setAttribute("type", "text");
newField.setAttribute("name", "s[x]np");
newField.setAttribute("id", "sx");
newField.setAttribute("class", "st form-text");
newField.setAttribute("value", getValue);

$('#addithere').append(newField);
$(newField).before('<label>Label</label>');
var secondItem = "Section: "+newField;
$('#addithere').append(secondItem);
});

Why don't try $(newField).before('<label>Your label</label>'); ?


Edit:
Maybe you don't have the $(document).ready(function(){....});
I tried your code, without this - it didn't work... But with it, it worked!

$(document).ready(function(){
var getValue = $('#newField').val();
var newField = document.createElement("input");
newField.setAttribute("type", "text");
newField.setAttribute("name", "s[x]np");
newField.setAttribute("id", "sx");
newField.setAttribute("class", "st form-text");
newField.setAttribute("value", getValue);

$('#addithere').append(newField);
$(newField).before('<label>Label</label>');
var secondItem = "Section: "+newField;
$('#addithere').append(secondItem);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文