繁体   English   中英

当JavaScript动态添加行时,单选按钮功能无法正常工作

[英]Radio button function not working properly when rows dynamically added by javascript

我正在通过json从数据库中获取值,并且同时添加了带有3个选项的单选按钮。 但是,当尝试进行选择时,我面临以下问题。

如果您运行代码并尝试为所有3个选项选择“绿色”,则该选项会不断从第一行跳到另一行。 如果要为所有3个选择绿色,则不要选择绿色。

我的目标是使用3个选项之一更新数据库,并将值存储在json对象中作为状态。

我的目的是从3个选项中进行选择,然后将其传递回数据库并进行更新。 请帮忙!

 type = "text/javascript" > $(document).ready(function() { var appStatus = [{ "category": "Overall Status", "status": "0" }, { "category": "System Availability", "status": "0" }, { "category": "Whatever 2", "status": "0" }]; var tr; for (var i = 0; i < appStatus.length; i++) { tr = $('<tr/>'); tr.append("<td>" + appStatus[i].category + "</td>"); tr.append('<tr id="row' + i + '"><td><input type="radio" name="inlineRadioOptions []" value="1" checked="checked" id="inlineRadio1"><font color="green">Green &emsp;</font><label class="radio-inline"><input type="radio" name="inlineRadioOptions1 []" id="inlineRadio2" value="YELLOW"><font color="yellow">Yellow &emsp;</font></label><label class="radio-inline"><input type="radio" name="inlineRadioOptions1 []" id="inlineRadio3" value="Red"> <font color="red">Red</font></label><td></tr>'); $('table').append(tr); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table table-hover"><thead> <th>Category</th> <th>Status</th> </thead> </table> 

您只需要给它们起相同的名字即可,如下面的代码片段所示。

然后,如果要获取新值,可以遍历tr:

var new_status = [];
$('.table tbody tr').each(function(){
   new_status.push( {category: $(this).find('td:eq(0)').text(),status: $(this).find(':radio:checked').val()});
});

希望这可以帮助。

 type = "text/javascript" > $(document).ready(function() { var appStatus = [{ "category": "Overall Status", "status": "0" }, { "category": "System Availability", "status": "0" }, { "category": "Whatever 2", "status": "0" }]; var tr; for (var i = 0; i < appStatus.length; i++) { tr = $('<tr/>'); tr.append("<td>" + appStatus[i].category + "</td>"); tr.append('<tr id="row' + i + '"><td><input type="radio" name="inlineRadioOptions_' + i + '[]" value="1" checked="checked" id="inlineRadio1"><font color="green">Green &emsp;</font><label class="radio-inline"><input type="radio" name="inlineRadioOptions_' + i + '[]" id="inlineRadio2" value="YELLOW"><font color="yellow">Yellow &emsp;</font></label><label class="radio-inline"><input type="radio" name="inlineRadioOptions_' + i + '[]" id="inlineRadio3" value="Red"> <font color="red">Red</font></label><td></tr>'); $('table').append(tr); } $('#result').on('click',function(){ var new_status = []; $('.table tbody tr').each(function(){ new_status.push( {category: $(this).find('td:eq(0)').text(),status: $(this).find(':radio:checked').val()}); }); console.log(new_status); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table table-hover"> <thead> <th>Category</th> <th>Status</th> </thead> </table> <button id="result">Result</button> 

暂无
暂无

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

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