开发者

jquery form labels? Do they not exist?

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("Labe开发者_JAVA百科l");
...

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

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

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

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


$('#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/>'));


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);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜