我有一个表单需要根据用户的ZIP在上面的数字字段中显示特定的选择选项(“位置”)。在这个例子中,我需要选项“超出范围”来隐藏和“In Range”,当用户在输入中输入“12345”时显示。基于输入值的jQuery显示/隐藏选项
这是我的HTML:
<!--Zip-->
<div class="zip-field">
<label for="zip">Zip Code</label>
<input type="number" id="zip" name="zip" />
</div>
<!--Location-->
<div class="location-field">
<label for="location">Location</label>
<select id="location" name="location">
<option value="In Range">In Range</option>
<option value="Out of Range">Out of Range</option>
</select>
</div>
这是我的jQuery:
$('#zip').on('change',function(){
if ($(this).val() == "12345") {
$("#location option[value='In Range']").show();
$("#location option[value='Out of Range']").hide();
}
});
应该很简单,但没有雪茄。
隐藏选项元素不具备跨浏览器的支持......你需要将其删除 –