如果选择了某个选项,则使用Jquery禁用"提交"按钮

Nol*_*lan 1 forms jquery field

我有一个简单的表单,有四个选择字段.如果在任何表单域中选择"是",则需要禁用"提交"按钮并显示隐藏的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>
Run Code Online (Sandbox Code Playgroud)

感谢您对此的帮助.我是一个jquery新手,我尽力学习.

Mik*_*ace 5

我不完全理解你建立表单的方式和你的要求的业务逻辑,但......基于你在这里所说的内容,我会做的.

  1. 写函数检查所有选项,更改禁用/启用提交,显示/隐藏div
  2. 加载时调用函数
  3. 调用函数以在每次选项值更改时检查所有选项

.

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");
  };
}
Run Code Online (Sandbox Code Playgroud)

稍加修改的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>
Run Code Online (Sandbox Code Playgroud)

示例网页:

http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-disable-submit-dynamically.html

负载:

替代文字

所有选项'不'只有一个:

替代文字

所有选项'是':

替代文字