我有一个包含四个选择字段的简单表单。如果在任何表单字段上选择“是”,则需要禁用提交按钮并显示隐藏的div。这是我的标记示例:
<form>
I have read the information on the product(s)<select name="field4"> <option value="Yes">Yes</option> <option value="No">No</option> </select>
Do you have any allergies to any ingredients in product(s)?
<select name="field5"><option value="Yes">Yes</option> <option value="No">No</option> </select>
Are you pregnant?
<select name="field6"> <option value="Yes">Yes</option> <option value="No">No</option> </select>
Have you ever used this type of product and had undesirable results?
<select name="field7"> <option value="Yes">Yes</option> <option value="No">No</option> </select>
<input name="cmdSubmit" type="submit" value="Submit" />
</form>
<div style="display:none;">Hidden div only to appear if any of the four dropdowns are marked YES.</div>
感谢您对此的帮助。我是一个jquery新手,我尽力学习。
答案 0 :(得分:5)
我并不完全理解您构建表单的方式和您的要求的业务逻辑,但......基于您在此处所说的内容,我会做的。
checkOptions();
$("select").change(checkOptions);
function checkOptions() {
var yesFound = false;
$("select").each(function(index, element) {
if ( $(element).val() == "Yes" ) {
yesFound = true;
}
});
if (yesFound) {
$("#hidden-div").show();
$("input[type=Submit]").attr("disabled","disabled");
} else {
$("#hidden-div").hide();
$("input[type=Submit]").removeAttr("disabled");
};
}
稍加修改的HTML:
<form>
<select name="field4">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select> I have read the information on the product(s)<br/>
<select name="field5">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select> Do you have any allergies to any ingredients in product(s)?<br/>
<select name="field6">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select> Are you pregnant?<br/>
<select name="field7">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select> Have you ever used this type of product and had undesirable results?<br/>
<br/>
<input name="cmdSubmit" type="submit" value="Submit" />
</form>
<div id="hidden-div" style="display:none;">Hidden div only to appear if any of the four dropdowns are marked YES.</div>
加载时
所有选项'不'只有一个:
所有选项'是':
答案 1 :(得分:1)
将所有选择(下拉列表)设为“foo”类。将“是”选项设为“是”。
$("select.foo option.yes:selected").empty()
表示“未选择标记为”是“的下拉选项”
所以
$("select.foo").change(function() {
if(!$("select.foo option.yes:selected").empty()) {
/* Do something here, maybe use BlockUI JS library */
}
});
答案 2 :(得分:0)
var selects = $('select').change(function(){ //on change of any select
var hasYes = false;
selects.each(function(){ // check all selects
if($(this).val() == 'Yes') //for 'Yes'
hasYes = true;
});
if(hasYes){ // show div disable submit
$('input:submit').attr('disabled','disabled');
$('div.hidden').show();
}
else{
$('input:submit').removeAttr('disabled');
$('div.hidden').hide(); // add class hidden to the div
}
});
答案 3 :(得分:0)
您可以将选择的类属性设置为类似“选项”的内容,如下所示:
<select name="field5" class="choices"...
<select name="field6" class="choices"...
并做这样的事情:
$(function() {
$(".choices").change(function() {
var anyYeses = false;
$(".choices").each(function() {
if($(this).val() == "Yes") {
anyYeses = true;
}
});
if(anyYeses) {
$("input[name=cmdSubmit]").attr('disabled', 'disabled'); //disables
}
else {
$("input[name=cmdSubmit]").removeAttr('disabled'); //enables
}
});
});