Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.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
Jquery 在表格行内的div内插入_Jquery_Html_Dynamic_Html Table - Fatal编程技术网

Jquery 在表格行内的div内插入

Jquery 在表格行内的div内插入,jquery,html,dynamic,html-table,Jquery,Html,Dynamic,Html Table,我有一张桌子,桌子上的div是交替排列的。要在这些div中添加表 请检查并为我更新 <table> <tr> <td>hi</td> </tr> <tr> <td>foo 1</td> <td> <input type="button" value="Remove" id="remove1

我有一张桌子,桌子上的div是交替排列的。要在这些div中添加表

请检查并为我更新

<table>
    <tr>
        <td>hi</td>
    </tr>
    <tr>
        <td>foo 1</td>
        <td>
            <input type="button" value="Remove" id="remove1" />
        </td>
    </tr>
    <tr>
        <td>
            <div></div>
        </td>
    </tr>
    <tr>
        <td>foo 2 </td>
        <td>
            <input type="button" value="Remove" id="remove2" />
        </td>
    </tr>
    <tr>
        <td>
            <div></div>
        </td>
    </tr>
</table>

$("table tr input").on('click', function(e){
    alert($(this).closest('td').parent()[0].sectionRowIndex);
    alert($(this).closest("tr").index()+1);
    var row = $(this).closest("tr").index()+1;
    $('table tr:row').find('div').append("<table><tr><td>hi</td></tr></table>");
});

你好
富1
富2
$(“表tr输入”)。在('click',函数(e){
警报($(this).closest('td').parent()[0].sectionRowIndex);
警报($(this).closest(“tr”).index()+1);
var row=$(this).closest(“tr”).index()+1;
$('table tr:row')。查找('div')。追加('hi');
});
您必须使用在该位置插入行,并尝试检查div是否存在
虽然你的问题不太清楚,但还是试一试

$("table tr input").on('click', function(e){
    var row = $(this).closest("tr").index() + 1; // Get index 
    if($('table tr').eq(row).find('div').length > 0); // Check if alternate row contains div
        $('table tr').eq(row).find('div').append("<table><tr><td>hi</td></tr></table>"); // Append table to div
});
文件:



  • 希望你需要这个

    如果有与输入相同数量的div,则可以使用相同的索引:

    $("table tr input").on('click', function(e){
        var row = $("table tr input").index(this);
        $("table div").eq(row).append("<table><tr><td>hi</td></tr></table>");
    });
    
    $(“表tr输入”)。在('click',函数(e){
    var行=$(“表tr输入”).index(此);
    $(“表div”).eq(行).append(“hi”);
    });
    
    那么您的问题是什么,代码正在工作,对吗?[Fiddle](jsfiddle.net/zwHK4/36/)不适用于多行。这对我有帮助。。但如果单击加载不同的带有文本框的表,然后将这些值返回到所需的div表中。。。这就是我寻找索引的原因
    $("table tr input").on('click', function(e){
        var row = $("table tr input").index(this);
        $("table div").eq(row).append("<table><tr><td>hi</td></tr></table>");
    });