1

我有多个带有子字段的 div。这些子字段共享相似的名称。我正在尝试显示特定的子字段。但我只得到第一个 div 的子字段。
它与子字段的name
HTML有什么关系吗

 <div class="row childFields" id="parent-question1">
        <p class="subtitle">Give full details here: </p>
        <div class="col-md-6 colChildFields" style="display:none" name="condition">
            <label class="pocLabel">Nature of Condition</label>
            <input type="text" name="condition" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="datesAndDuration">
            <label class="pocLabel">Dates and Duration</label>
            <input type="text" name="datesAndDuration" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="doctorName">
            <label class="pocLabel">Name Of Doctor</label>
            <input type="text" name="doctorName" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="hospitalName">
            <label class="pocLabel">Name of Hospital</label>
            <input type="text" name="hospitalName" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="hospitalAddress">
            <label class="pocLabel">Address of Hospital</label>
            <input type="text" name="hospitalAddress" class="form-control">
        </div>
    </div>
    <div class="row childFields" id="parent-question2">
        <p class="subtitle">Give full details here: </p>
        <div class="col-md-6 colChildFields" style="display:none" name="condition">
            <label class="pocLabel">Nature of Condition</label>
            <input type="text" name="condition" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="datesAndDuration">
            <label class="pocLabel">Dates and Duration</label>
            <input type="text" name="datesAndDuration" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="doctorName">
            <label class="pocLabel">Name Of Doctor</label>
            <input type="text" name="doctorName" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="hospitalName">
            <label class="pocLabel">Name of Hospital</label>
            <input type="text" name="hospitalName" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="hospitalAddress">
            <label class="pocLabel">Address of Hospital</label>
            <input type="text" name="hospitalAddress" class="form-control">
        </div>
    </div>
    <div class="row childFields" id="parent-question3">
        <p class="subtitle">Give full details here: </p>
        <div class="col-md-6 colChildFields" style="display:none" name="condition">
            <label class="pocLabel">Nature of Condition</label>
            <input type="text" name="condition" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="datesAndDuration">
            <label class="pocLabel">Dates and Duration</label>
            <input type="text" name="datesAndDuration" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="doctorName">
            <label class="pocLabel">Name Of Doctor</label>
            <input type="text" name="doctorName" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="hospitalName">
            <label class="pocLabel">Name of Hospital</label>
            <input type="text" name="hospitalName" class="form-control">
        </div>
        <div class="col-md-6 colChildFields" style="display:none" name="hospitalAddress">
            <label class="pocLabel">Address of Hospital</label>
            <input type="text" name="hospitalAddress" class="form-control">
        </div>
    </div>

jQuery

var childFieldDivId = '#parent-'+fieldName;
$(childFieldDivId).find('div').each(function(i, childField){
  $(childField).show();
});
4

4 回答 4

1

试试这个:

var childFieldDivId = '#parent-'+fieldName;
$(childFieldDivId).find('div').each(function(index, value){
  $(this).show();
});

fiddle

于 2013-08-28T18:29:11.003 回答
1

试试这个:

var childFieldDivId = '#parent-'+fieldName;
$('.subtitle').on('click', function(){ // not sure you need onclick handler
    $(this).siblings().show();
});

演示在这里

于 2013-08-28T18:32:38.970 回答
0

代替循环和调用show(),调用show()整个选择器 - DEMO

var childFieldDivId = '#parent-'+fieldName;
$(childFieldDivId).find('div').show();
于 2013-08-28T18:28:55.183 回答
0

如果您想显示该 div 的所有子项,请尝试此操作

var childFieldDivId = '#parent-'+fieldName;
$(childFieldDivId).children('div').show();
于 2013-08-28T18:31:44.117 回答