use*_*146 15 forms jquery jquery-ui dom-manipulation jquery-ui-datepicker
我需要动态创建标签和文本字段,还包括文本字段的datepicker.我需要这样的东西:
<label for="from">From </label> <input type="text" id="from" name="from" />
Run Code Online (Sandbox Code Playgroud)
我在jQuery中尝试过类似的东西:
var label = $("<label>").attr('for', "from");
label.html('Date: ' +
'<input type="text" id="from name="from" value="">');
$(function() {
$("#from").datepicker();
});
Run Code Online (Sandbox Code Playgroud)
这个不知何故不会创建标签和文本字段.我不确定我错过了什么.
编辑
更确切地说,我在portlet中使用它,并且我在jsp页面中没有body标签.所以当我调用函数追加到body时它不会.
kap*_*apa 25
像这样的东西会起作用:
//Create the label element
var $label = $("<label>").text('Date:');
//Create the input element
var $input = $('<input type="text">').attr({id: 'from', name: 'from'});
//Insert the input into the label
$input.appendTo($label);
//Insert the label into the DOM - replace body with the required position
$('body').append($label);
//applying datepicker on input
input.datepicker();
Run Code Online (Sandbox Code Playgroud)
请注意,for
如果输入元素位于标签内,则不必在标签上使用该属性.所以使用其中一个:
<label><input id="x1"></label>
<label for="x1"></label> <input id="x1">
Run Code Online (Sandbox Code Playgroud)