【发布时间】:2016-05-26 15:21:46
【问题描述】:
我有一个表单,其中 div 根据下拉框中的选择显示/隐藏。通过 jquery 使用 .show 和 .hide 可以很好地显示/隐藏字段,但是这些表单上的输入需要是必需的。发生的情况是,当其中一个字段被隐藏时,表单将不会t 提交 - 很可能是因为这个“隐藏”字段仍然需要。
当在下拉列表中选择某个项目时,有没有办法一起禁用/删除此字段而不是隐藏 div?我尝试使用 .attr("disabled", true) 没有运气。任何帮助表示赞赏。
JQuery sn-p:
$('#productpr1').on('change',function() {
if(this.value=='PRODUCT1')
{
$('.initial').show();
$('.name').show();
$('.mono').hide();
$('.comment').show();
}
else if (this.value=='PRODUCT2')
{
$('.initial').hide();
$('.name').hide();
$('.mono').show();
$('.comment').show();
}
else if (this.value=='PRODUCT3')
{
$('.initial').show();
$('.name').show();
$('.mono').show();
$('.comment').show();
}
else
{
$('.initial').show();
$('.name').show();
$('.mono').show();
$('.comment').show();
}
});
HTML:
<select name="productpr1" id="productpr1">
<option value="Please Select:0">Please select your product</option>
<option value="PRODUCT1">PRODUCT1</option>
<option value="PRODUCT2">PRODUCT2</option>
<option value="PRODUCT3">PRODUCT3</option>
</select>
<br>
<br>
<div class="initial">
<div id="Profont"><b>Initial:</b>
</div>
<input name="product1[]" type="text" required id="thisone" size="5">
</div>
<div class="name">
<div id="Profont"><b>Name:</b>
<br>
</div>
<input name="product1[]" type="text" required id="thisone" size="5">
</div>
<div class="mono">
<div id="Profont"><b>Mono Initials:</b>
<br>
</div>
<input name="product1[]" type="text" required id="thisone" size="5">
</div>
<div class="comment">
<div id="Profont"><b>Additional Comments:</b>
<br>
</div>
<input type="text" name="product1[]" size="10" id="comments">
</div>
<input type="submit" value="Add to Cart" name="Cart" class="INDVbutton" />
【问题讨论】:
标签: jquery html forms show-hide