[英]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
: 我在下拉菜单的
first
和second
部分添加了一些类:
<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.