如何在 jQuery 中创建动态复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5949169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:04:52  来源:igfitidea点击:

How do i create dynamic Checkbox in jQuery?

jquerydynamiccheckbox

提问by user746679

I need to create dynamic checkbox using jQuery. How do i do that? Any code snippet will be helpful.

我需要使用 jQuery 创建动态复选框。我怎么做?任何代码片段都会有所帮助。

采纳答案by Deviprasad Das

See the following link: How to create a radio button dynamically with jQuery?

请参阅以下链接: 如何使用 jQuery 动态创建单选按钮?

Here it is showing how to create radio button using jQuery, you can change the code to create Checkbox.

这里展示了如何使用 jQuery 创建单选按钮,您可以更改代码以创建复选框。

回答by fearofawhackplanet

$('#containerId').append('<input type="checkbox" name="myCheckbox" />');

where containerIdis the id of the DOM element you want to add the checkbox to.

containerId要将复选框添加到的 DOM 元素的 id在哪里。

Or alternative syntax...

或替代语法...

$('#containerId')
    .append(
       $(document.createElement('input')).attr({
           id:    'myCheckbox',
           name:  'myCheckbox',
           value: 'myValue',
           type:  'checkbox'
       })
    );