首页 > 解决方案 > 由父级返回到不同的子级 JQuery

问题描述

这是我的带有不同数量复选框的动态页面的代码。我想知道的是我设置的“.parent().children()”属性是否可行。我不断返回 [Object Object]。你能帮忙的话,我会很高兴。


<div>
            <input name='input1' id='src-1' value = 'input1' type='checkbox'/><br />
            <input name='depnum1' id='depnum-1' value = '' type='text'/><br />
            <input name='depdate1' id='depdate-1' value = '' type='text'/>
        </div>
<script>
    $(document).ready(function() {
        $("#SubDeposit").click(function(){  
            $('input[type=checkbox]').each(function (){
                if($(this).prop('checked')) {
                    //variables
                    var fileLocation = $(this).attr('id');                  
                    var fileLocation = fileLocation.substring(4);
                    alert(fileLocation);            
                    //Variables for inputs that relate to the chekcbox
                    depnum  = ($(this).parent().children('depnum-'+fileLocation));
                    depdate = ($(this).parent().children("depdate-"+fileLocation));
                    alert(depnum);
                    alert(depdate);     
                    console.log(depnum);    
                    console.log(depdate);   
                }
            });
        });
    });
    </script>

标签: jquery

解决方案


我认为问题在于您的选择器。您想找到具有 id=some 值的元素。在这种情况下, id 的选择器是"#text"如此,正确的 jquery 调用将是

depnum  = ($(this).parent().find('#depnum-'+fileLocation));
depdate = ($(this).parent().find("#depdate-"+fileLocation));

推荐阅读