Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.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 jquery如何使用tr:nth子(i)构造每个or for循环构造_Javascript_Jquery_Css_For Loop - Fatal编程技术网

Javascript jquery如何使用tr:nth子(i)构造每个or for循环构造

Javascript jquery如何使用tr:nth子(i)构造每个or for循环构造,javascript,jquery,css,for-loop,Javascript,Jquery,Css,For Loop,我有以下jQuery代码段: $('.product tr:nth-child(2) .knop',window.parent.document).bind("click", function(){ $('#edit-submitted-data-cursus').val($('.product tr:nth-child(2) .cursus a',window.parent.document).html()) $('#edit-submitted-data-cursusdatu

我有以下jQuery代码段:

$('.product tr:nth-child(2) .knop',window.parent.document).bind("click", function(){
    $('#edit-submitted-data-cursus').val($('.product tr:nth-child(2) .cursus a',window.parent.document).html())
    $('#edit-submitted-data-cursusdatum').val($('.product tr:nth-child(2) .datum',window.parent.document).html())
    $('#edit-submitted-data-opleidingscode').val($('.product tr:nth-child(2) .code',window.parent.document).html())
    $('#edit-submitted-data-cursuslocatie').val($('.product tr:nth-child(2) .loc',window.parent.document).html())
    $('#edit-submitted-data-cursustarief').val($('.product tr:nth-child(2) .tarief',window.parent.document).html())
}); 

$('.product tr:nth-child(3) .knop',window.parent.document).bind("click", function(){
    $('#edit-submitted-data-cursus').val($('.product tr:nth-child(3) .cursus a',window.parent.document).html())
    $('#edit-submitted-data-cursusdatum').val($('.product tr:nth-child(3) .datum',window.parent.document).html())
    $('#edit-submitted-data-opleidingscode').val($('.product tr:nth-child(3) .code',window.parent.document).html())
    $('#edit-submitted-data-cursuslocatie').val($('.product tr:nth-child(3) .loc',window.parent.document).html())
    $('#edit-submitted-data-cursustarief').val($('.product tr:nth-child(3) .tarief',window.parent.document).html())
});

$('.product tr:nth-child(4) .knop',window.parent.document).bind("click", function(){
    $('#edit-submitted-data-cursus').val($('.product tr:nth-child(4) .cursus a',window.parent.document).html())
    $('#edit-submitted-data-cursusdatum').val($('.product tr:nth-child(4) .datum',window.parent.document).html())
    $('#edit-submitted-data-opleidingscode').val($('.product tr:nth-child(4) .code',window.parent.document).html())
    $('#edit-submitted-data-cursuslocatie').val($('.product tr:nth-child(4) .loc',window.parent.document).html())
    $('#edit-submitted-data-cursustarief').val($('.product tr:nth-child(4) .tarief',window.parent.document).html())
});

    etc.    
我有54个这样的函数。这是大量的冗余。所以我希望有一个循环,但直到现在我还没有能够成功。应该不会太难,但它就在我头上:-(

有人能帮我吗?已经谢谢了!

这应该可以:

var productsRows = $('.product tr');
productsRows.on('click', '.knop button', function(evt){
    var currentTarget = $(evt.currentTarget);
    var currentRow = currentTarget.parents('tr');

    $('#edit-submitted-data-cursus').val(currentRow.find('.cursus').html());
    $('#edit-submitted-data-cursusdatum').val(currentRow.find('.datum').html());
    $('#edit-submitted-data-opleidingscode').val(currentRow.find('.code').html());
    $('#edit-submitted-data-cursuslocatie').val(currentRow.find('.loc').html());
    $('#edit-submitted-data-cursustarief').val(currentRow.find('.tarief').html());
});
这是一把小提琴:

这也就不需要从
.knop
单元格开始区分表首(您可能想在前后使用
而不是
),因为它只监听
.knop
单元格中的按钮点击(如果没有按钮,事件无法触发).

试试这个:

for (var i = 1; i <= 54; i++) {
    $('.product tr:nth-child(' + i + ') .knop',window.parent.document).bind("click", function() {
        $('#edit-submitted-data-cursus').val($('.product tr:nth-child(' + i + ') .cursus a', window.parent.document).html());
        $('#edit-submitted-data-cursusdatum').val($('.product tr:nth-child(' + i + ') .datum', window.parent.document).html());
        $('#edit-submitted-data-opleidingscode').val($('.product tr:nth-child(' + i + ') .code', window.parent.document).html());
        $('#edit-submitted-data-cursuslocatie').val($('.product tr:nth-child(' + i + ') .loc', window.parent.document).html());
        $('#edit-submitted-data-cursustarief').val($('.product tr:nth-child(' + i + ') .tarief', window.parent.document).html());
    });
}

for(var i=1;i)这将不起作用,因为您从第n个子项(0)
开始,到第n个子项(53)结束@mvuajua:PO可以更改边框,重要的是在这种情况下如何创建循环。感谢您的帮助,但不幸的是,没有一个解决方案适合我。我也认为简单的for循环可以做到这一点,但它不能做到,我也无法理解。当我不使用循环,只需填写tr:nth-child()的位置时,代码就可以工作了有一个数字,但当然每行的值都是相同的。还有其他人吗?你尝试过我提出的第二种解决方案吗?请用你当前的代码更新你的帖子(如果可能的话,也提供一个fiddle,可能问题出在其他地方)是的,我也尝试过。我制作了一个简化的代码示例(我不确定如何使用fiddle)在上,如果查看de源代码,您将看到活动脚本。这仅适用于“Access”的表行。当然,这对其他行不起作用。注释掉的代码是一个简单的for循环,它不起作用:-(.也许你想看一看?我已经用我从你的代码中添加的代码更新了我的答案。希望这对你有用。太棒了!!!真的很有用。谢谢!!最初表单输入是在一个iframe中。我现在将尝试使用你的代码使其生效。