简体   繁体   English

如何根据下拉值禁用/启用表单

[英]How can i disable/enable form based on dropdown value

I have two forms on the same page. 我在同一页面上有两种形式。 I want to enable/disable a form element based on the drop down value selected on the first form. 我想基于在第一个表单上选择的下拉值来启用/禁用表单元素。

On the first drop down When option 1 selected or <option value="">Select</option> I want to disable the second dropdown from the first form or <select name="tractor" ..> and everything on 2nd Form form. 在第一个下拉菜单中选择选项1或<option value="">Select</option>我想从第一个表单或<select name="tractor" ..>以及第二个表单表单中的所有内容禁用第二个下拉列表。

if from <select name="tire"> : Rear Tire or Front Tire selected but Nothing selected in the second dropdown i want to enable <select name="tractor"> and keep 2nd Form disable. 如果来自<select name="tire"><select name="tire"> Rear Tire或“ Front Tire但在第二个下拉列表中未选择任何内容,我想启用<select name="tractor">并保持“ 第二个表单”禁用。

1st Form 第一种形式

<form method="post">
<select name="tire">
    <option  value="">Select</option>
    <option  value="rear">Rear Tire</option>
    <option  value="front">Front Tire</option>
</select> 
 <select name="tractor">
    <option value="">select Type</option>
    <option value="2wd">2WD</option>
    <option value="mfwd">MFWD</option>
    <option value="4wd">4WD</option>
 </select>
 <input type="submit" value="Go" name="cmd_submit" />

2nd Form // want to be disabled when page loads 第二种形式 //要在页面加载时被禁用

 <form method="post" id="vehicle" action="page.php">
      .
      .
      .
 </form>

Simply check the value of the first dropdown. 只需检查第一个下拉菜单的值即可。 I added some classes to the dropdowns first and second : 我在下拉菜单的firstsecond部分添加了一些类:

<h3> First form </h3>
<form method="post" class="firstform">
  <select name="tire" class="first">
    <option  value="">Select</option>
    <option  value="rear">Rear Tire</option>
    <option  value="front">Front Tire</option>
  </select> 
  <select name="tractor" class="second" disabled>
    <option value="">select Type</option>
    <option value="2wd">2WD</option>
    <option value="mfwd">MFWD</option>
    <option value="4wd">4WD</option>
  </select>
  <input type="submit" value="Go" name="cmd_submit" />
</form>

<h3> Second form </h3>
<form method="post" id="vehicle" action="page.php" class="secondform">
   <label>A form with no fields is useless: </label>
   <input type="text" placeholder="so here a field" class="secondformfield" />  <br />
   <label>This is a dropdown: </label>
   <select name="tractor" class="secondformfield">
    <option value="">...</option>
    <option value="2wd">With an option</option>
    <option value="mfwd">And another option</option>
   </select>  <br />
   <textarea class="secondformfield">some text</textarea>
   <input type="button" value="And away!" name="cmd_away" class="secondformfield"/> 
</form>

And the Js 和Js

$(document).ready(function(){
    $(".secondform .secondformfield").prop("disabled",true);

    $(".first").change(function(){
      if($(this).val() !== ""){
        $(".second").prop("disabled",false);
      }
      else{
        $(".second").prop("disabled",true);
      }

      checkIfEverythingIsFilledIn();
    });

    $(".second").change(function(){
      checkIfEverythingIsFilledIn();
    });

    function checkIfEverythingIsFilledIn(){
      if($(".first").val() !== "" && $(".second").val() !== ""){
        $(".secondform .secondformfield").prop("disabled",false);
        $(".secondform").attr("method","post").attr("action","page.php");
      }
      else{
        $(".secondform .secondformfield").prop("disabled",true);
        $(".secondform").removeAttr("method").removeAttr("action");
      }
    }
});

For preventing the submit of the form there are multiple solutions. 为了防止提交表格,有多种解决方案。 The submitbutton will be disabled automatically because it is also of type input . 提交按钮也将被自动禁用,因为它也是input类型。 Another option is to remove/add the attributes of the form like I did above and in a JsFiddle 另一个选择是像上面和在JsFiddle中一样删除/添加表单的属性

Edit: Updated js code by a good advise of @WorldFS 编辑:通过@WorldFS的一个很好的建议更新了js代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于所选下拉列表中的值启用和禁用表单控件 - How to Enable and Disable Form Controls based on values in selected dropdown list 根据下拉值启用/禁用复选框 - Enable/Disable checkbox based on value of dropdown 如何根据上一个下拉值的选择启用/禁用第二个下拉列表值 - How to enable/disable 2nd dropdownlist value based on the selection of the previous dropdown value 如何基于另一个下拉列表启用和禁用下拉列表项 - How to enable and disable dropdown list items based on another dropdown list 如何使用 AngularJS 基于下拉选择值禁用输入文本框? - How can i disable an input text box based on dropdown selection value using AngularJS? jQuery:基于下拉值的启用/禁用不适用于动态生成的输入 - Jquery: Enable/Disable based on value of dropdown is not working for dynamically generated inputs 使用javascript根据其他下拉列表中的选定值启用或禁用下拉列表 - Enable or disable dropdowns based on selected value in other dropdown using javascript 我如何禁用/启用Ajax请求中的表单提交 - How can I disable/enable form submit in an ajax request 如何更改表格值基于所选下拉值的操作 - How can i change Form value Action based on dropdown value selected 如何基于下拉选择禁用/启用动态表中的复选框 - How to disable/enable checkboxes in dynamic table based on dropdown selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM