试过多次得到第二个输入表单只显示在正确的选择作出时,仍然会显示任何一种方式。 刚来js,不确定自己做错了什么。
由于我是html新手,它可能与我的ID有关。 对js完全陌生,但用过dart,看起来也差不多,但我不能指出问题所在。
null
$("#job-type").change(function(){
if ($(this).val() == "internals-civil") {
$('#telecom-internal-civil').show();
$('#internal-civil').attr('required');
$('#internal-civil').attr('data-error');
} else {
$('#telecom-internal-civil').hide();
$('#internal-civil').removeAttr('required');
$('#internal-civil').removeAttr('data-error');
}
});
$("#job-type").trigger("change");
<div class="form-body">
<div class="contact-labels">
<label for="job-type">Job Type</label>
</div>
<div>
<select name="job-type" id="job-type" required>
<option value="" disabled selected value selected
>Select one</option
>
<option value="internals-civil"
>Telecom Internals & Civil</option
>
<option value="fencing">Fencing</option>
</select>
</div>
</div>
<div class="form-body form-body-telecom" id="telecom-internal-civil">
<div class="contact-labels">
<label for="internal-civil">Job Type</label>
</div>
<div>
<select name="#" id="internal-civil">
<option value="" disabled selected value selected
>Select one</option
>
<option value="#">Blah</option>
<option value="#">Flah</option>
</select>
</div>
</div>
null
这是jquery而不是js,您没有包括
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
工作示例:
null
$("#job-type").change(function(){
if ($(this).val() == "internals-civil") {
$('#telecom-internal-civil').show();
$('#internal-civil').attr('required');
$('#internal-civil').attr('data-error');
} else {
$('#telecom-internal-civil').hide();
$('#internal-civil').removeAttr('required');
$('#internal-civil').removeAttr('data-error');
}
});
$("#job-type").trigger("change");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-body">
<div class="contact-labels">
<label for="job-type">Job Type</label>
</div>
<div>
<select name="job-type" id="job-type" required>
<option value="" disabled selected value selected
>Select one</option
>
<option value="internals-civil"
>Telecom Internals & Civil</option
>
<option value="fencing">Fencing</option>
</select>
</div>
</div>
<div class="form-body form-body-telecom" id="telecom-internal-civil">
<div class="contact-labels">
<label for="internal-civil">Job Type</label>
</div>
<div>
<select name="#" id="internal-civil">
<option value="" disabled selected value selected
>Select one</option
>
<option value="#">Blah</option>
<option value="#">Flah</option>
</select>
</div>
</div>