使用JQuery在嵌套Div中选择元素

时间:2013-11-04 16:04:22

标签: javascript jquery jquery-selectors

我在名为item的Div中有以下html标记,我想选择所有元素(嵌套div内)并清除值。如下面给出的Jquery所示,我已经设法使用.children().each()访问每个Div中的元素。但问题是.children().each()一次从父div降低一级,所以我重复使用多个.children()的相同代码块来访问嵌套Div中的元素,任何人都可以建议我方法这样做而不重复N个嵌套div的代码。

html标记

<div class="item">
    <input type="hidden" value="1234" name="testVal">

    <div class="form-group" id="c1">
        <div class="controls ">
            <input type="text" value="Results" name="s1" maxlength="255" id="id2">
        </div>
    </div>
    <div class="form-group" id="id4">
        <input type="text" value="Results" name="s12" maxlength="255" id="id225">

        <div class="form-group" id="id41">
            <input type="text" value="Results" name="s12" maxlength="255" id="5">

            <div class="form-group" id="id42">
                <input type="text" value="Results" name="s12" maxlength="255" id="5">

                <div class="form-group" id="id43">
                    <input type="text" value="Results" name="s12" maxlength="255" id="id224">
                </div>
            </div>
        </div>
    </div>
</div>

我的Qjuery脚本

var row = $(".item:first").clone(false).get(0);
$(row).children().each(function () {
updateElementIndex(this, prefix, formCount);

if ($(this).attr('type') == 'text') {
    $(this).val('');
}
if ($(this).attr('type') == 'hidden' && ($(this).attr('name') != 'csrfmiddlewaretoken')) {
    $(this).val('');
}
if ($(this).attr('type') == 'file') {
    $(this).val('');
}
if ($(this).attr('type') == 'checkbox') {
    $(this).attr('checked', false);
}
$(this).remove('a');
});

// Relabel or rename all the relevant bits
$(row).children().children().each(function () {
updateElementIndex(this, prefix, formCount)

if ($(this).attr('type') == 'text') {
    $(this).val('');
}
if ($(this).attr('type') == 'hidden' && ($(this).attr('name') != 'csrfmiddlewaretoken')) {
    $(this).val('');
}
if ($(this).attr('type') == 'file') {
    $(this).val('');
}
if ($(this).attr('type') == 'checkbox') {
    $(this).attr('checked', false);
}
$(this).remove('a');
});

5 个答案:

答案 0 :(得分:1)

似乎这样的事情可以解决问题。您当然要更具体地说明您选择的项目。

$('.item').find('input').val('');

答案 1 :(得分:0)

尝试

var row = $(".item:first").clone(false).get(0);
$(row).find('input:not(:checkbox)').val('');
$(row).find('input:checkbox').prop('checked', false);

答案 2 :(得分:0)

$(".item :input:not([name=csrfmiddlewaretoken])").val('');
$(".item :checkbox").prop("checked", false);
$(".item a").remove();

答案 3 :(得分:0)

你可以使用find代替孩子,它将一直沿着dom遍历。

$(row).find('input').each(function () {

答案 4 :(得分:0)

试试这个

 var row = $(".item:first").clone(false).get(0);
     $(row).children().each(function () {
         var $this=$(this);
         clearelements($this);
         if(element.children().length > 0)
          {
           element.children().each(function(){
              recursive(this)
            clearelements(this);
           });
         }
     });


    function recursive(element)
     {

    if(element.children().length > 0)
     {
      element.children().each(function(){
      clearelements(this);
      });
     }
 }

      function clearelements(obj){
         if ($(obj).attr('type') == 'text') {
            $(obj).val('');
          }
         if ($(obj).attr('type') == 'hidden' && ($(this).attr('name') != 'csrfmiddlewaretoken'))   {
           $(obj).val('');
           }
           if ($(obj).attr('type') == 'file') {
           $(obj).val('');
            }
           if ($(obj).attr('type') == 'checkbox') {
           $(obj).attr('checked', false);
            }
           $(obj).remove('a');
         }

或者您只是一次可以访问它们

    $(".item :input:not([name=csrfmiddlewaretoken]).val('');
    $(".item :checkbox).prop('checked', false);
    $(".item a").remove();
相关问题