我有以下HTML:使用JQuery动态添加HTML的最佳方式是什么?
<div id="dynamicRadioButtons">
</div>
使用jQuery我想追加一个单选按钮与相应的标签上面的DIV。我想追加HTML的一个例子如下:
<input type="radio" id="dynamicRadio" name="radio" /><label for="dynamicRadio">My Label</label> <br>
因此,可以说我有一个当我点击的东西,就是所谓的和需要的参数ID(这是单选按钮的ID功能要创建)和LabelText的(这是标签的文本,以创建),我希望能够做这样的事情:
function OnClick(id, labelText){
// create a new radio button using supplied parameters
var $newRadioBtn = $('<input />').attr({type="radio", id=id, name="radio"});
// create a new radio button using supplied parameters
// TODO - set the value of the label to parameter: labelText
var $newLabel = $('<label />').attr({for=id});
// TODO - append the new radio button and label to the div and then append <br>...
$('#dynamicRadioButtons').append(/* The elements I want to append */);
}
有人可以帮助我与我的TODO位吗?我正在关注一个示例as per this page,但只是不太了解jQuery来添加标签等。此外,这是动态添加HTML元素的最佳方式吗?
我有一个烂摊子有关的jsfiddle以及与此解决方案,它是足以让我保持与工作,但这个是最好的办法想出了? http://jsfiddle.net/uNNMr/881/ –