简体   繁体   English

如何在单选按钮上正确注册点击事件?

[英]How to properly register a click event on a radio button?

I am dynamically creating HTML radio buttons where I am assigning the Id of the input tag to a variable. 我正在动态创建HTML单选按钮,在其中将输入标签的ID分配给变量。 I want to append a click event handler to these radio buttons using the Id I have assigned. 我想使用我分配的ID将click事件处理程序附加到这些单选按钮。 How do I properly use the Id I created to generate a click event? 如何正确使用我创建的ID生成点击事件? Right now, the event is not being triggered at all. 目前,该事件根本没有被触发。

generateDynamicHTML(function (structure) {
let keys = Object.keys(structure);
keys.forEach(function(key){
    let radioButton = $("<input type='radio' name='studentName' id=" + key + "/><label for=" + key + ">" + key + "</label>");
    radioButton.appendTo('#studentToggle')

    $("#" + key).click(function () {
        console.log(key);
    })
})

}) })

I am using the console.log to test if the method was being hit but I am getting empty results. 我正在使用console.log测试该方法是否被击中,但结果却是空的。 I know the keys are correct because the radio buttons are being created. 我知道键是正确的,因为正在创建单选按钮。

Any help would be appreciated. 任何帮助,将不胜感激。

Thank you. 谢谢。

The problem is that the id added is key/ not key . 问题是添加的id是key/不是key You should leave a space between " and the closing of the input tag. Or use template literals. 您应在"和输入标签的结束之间留一个空格。或使用模板文字。

See below 见下文

 const structure = { 'first': 1, 'second': 2 } let keys = Object.keys(structure); keys.forEach(function(key) { let radioButton = $("<input type='radio' name='studentName' id=" + key + " /><label for=" + key + ">" + key + "</label>"); // or template literals // let radioButton = $(`<input type='radio' name='studentName' id=${key} /><label for=${key}>${key}</label>`); radioButton.appendTo('#studentToggle') $("#" + key).click(function() { console.log(key); }) }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="studentToggle"> </div> 

If you use event delegation, you never have to add extra event handlers to options as long as their parent does not change: 如果使用事件委派,则只要父项不变,就不必在选项中添加额外的事件处理程序:

 const generateDynamicHTML = function( structure ) { return Object .keys( structure ) .map(function( key ) { return '<input type="radio" name="studentName" id="' + key + '"/><label for="' + key + '">' + key + '</label>'; }) .join( '' ); }; const fields = $( '#student_fields' ); // Add the click handler before any radios fields.on( 'click', 'input[type="radio"]', function( event ) { console.log( event.target.id ); }); // Add the radios, the click stil works fields.append( generateDynamicHTML({ "john": { "age": 21 }, "jane": { "age": 20 } })); // Add more radios fields.append( generateDynamicHTML({ "dave": { "age": 21 }, "joe": { "age": 19 } })); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <fieldset id="student_fields"> <legend>Students</legend> </fieldset> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM