jquery 表单标签?它们不存在吗?
我正在创建一个动态输入元素,我想为其设置标签...我已经尝试了很多方法...
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是创建新标签元素的正确语法。你应该使用这样的东西:
This is not the correct syntax for creating a new label element. You should be using something like this:
为什么不尝试
$(newField).before('');
?编辑:
也许您没有
$(document).ready(function(){....});
我尝试了你的代码,没有这个 - 它不起作用......但是有了它,它起作用了!
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!