Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击动态生成的表中的行时,选中其单选按钮_Javascript_Jquery - Fatal编程技术网

Javascript 单击动态生成的表中的行时,选中其单选按钮

Javascript 单击动态生成的表中的行时,选中其单选按钮,javascript,jquery,Javascript,Jquery,在我的网页上,我有一个按钮,它的点击会动态生成一个表格。表格的每一行都有一个单选按钮。现在,我试图在单击某一行时选中该行相应的单选按钮。 这是HTML Insert table here <button name="myreq" id="myreq" class="homeButtons">My Requests</button> <table class="newreqtable" id="myrequesttable"></table> 在此

在我的网页上,我有一个按钮,它的点击会动态生成一个表格。表格的每一行都有一个单选按钮。现在,我试图在单击某一行时选中该行相应的单选按钮。 这是HTML

Insert table here
<button name="myreq" id="myreq" class="homeButtons">My Requests</button>
<table class="newreqtable" id="myrequesttable"></table>
在此处插入表格
我的要求
以下是我试图实现的JS。我已经在“jsarray”中手动添加了值,以便于操作

$(“myreq”)
.按钮()
。单击(函数(){
var jsarray=新数组();
jsarray.push(“1”);
jsarray.push(“requestor1”);
jsarray.push(“approver1”);
jsarray.push(“待定”);
jsarray.push(“chrome”);
jsarray.push(“25”);
jsarray.push(“source1”);
jsarray.push(“dest1”);
jsarray.push(“2”);
jsarray.push(“请求者2”);
jsarray.push(“approver2”);
jsarray.push(“待定”);
jsarray.push(“firefox”);
jsarray.push(“25”);
jsarray.push(“source2”);
jsarray.push(“dest2”);
var表=“”;
表+=“SelectRequestIDRequestApproverStatusProductVersionSourceDestination”;
对于(var j=0;j

任何关于我可能做错了什么的想法都会很有帮助。谢谢

您需要在dom就绪时初始化事件

 $("#myrequesttable").html(table);
//bind event when dom is ready
$('#myrequesttable tr').click(function () {
$('#myrequesttable tr').removeClass("active");
$(this).addClass("active");
$(this).find('[name="requestradio"]').prop('checked', true);

您需要在dom就绪时初始化事件

 $("#myrequesttable").html(table);
//bind event when dom is ready
$('#myrequesttable tr').click(function () {
$('#myrequesttable tr').removeClass("active");
$(this).addClass("active");
$(this).find('[name="requestradio"]').prop('checked', true);

试试这个

$('body').on('click','#myrequesttable tr',function (){

    $('#myrequesttable tr').removeClass("active");
    $(this).addClass("active");
    $(this).find('[name="requestradio"]').prop('checked', true);
});
试试这个

$('body').on('click','#myrequesttable tr',function (){

    $('#myrequesttable tr').removeClass("active");
    $(this).addClass("active");
    $(this).find('[name="requestradio"]').prop('checked', true);
});

更改您的点击事件:

$('#myrequesttable tr').click(function () {
为此:

$('#myrequesttable').on('click', 'tr', function () {

查看您的所有
s
都是动态生成的,因此在加载页面时它们不存在,此问题的解决方案是将事件委托给页面dom中最接近的静态父级,即
#myrequesttable
表。所以你必须授权给这个


虽然您可以委托给
$(文档)
本身,但如果涉及性能,则应避免委托。

更改您的单击事件:

$('#myrequesttable tr').click(function () {
为此:

$('#myrequesttable').on('click', 'tr', function () {

查看您的所有
s
都是动态生成的,因此在加载页面时它们不存在,此问题的解决方案是将事件委托给页面dom中最接近的静态父级,即
#myrequesttable
表。所以你必须授权给这个


虽然您可以委托给
$(文档)
本身,但如果涉及性能,则应避免委托。

将代码修改为以下内容:

// Cache $("#myrequesttable")
var $myrequesttable = $("#myrequesttable");
$myrequesttable
    .on("click", "tr", function () {
        // Cache $(this)
        var $this = $(this);
        $myrequesttable.find("tr").removeClass("active");
        $this.addClass("active");
        $this.find('input:radio[name="requestradio"]').prop('checked', true);
    });

将代码修改为以下内容:

// Cache $("#myrequesttable")
var $myrequesttable = $("#myrequesttable");
$myrequesttable
    .on("click", "tr", function () {
        // Cache $(this)
        var $this = $(this);
        $myrequesttable.find("tr").removeClass("active");
        $this.addClass("active");
        $this.find('input:radio[name="requestradio"]').prop('checked', true);
    });