选择选项:jQuery、大小写和显示/隐藏表单字段
根据选择控件的值显示/隐藏表单字段的最有效方法是什么?我有一个选择控件和三个输入字段,它们根据您工作的业务类型显示/隐藏。
<select id="business-type">
<option value="1">Sole Trader</option>
<option value="2">Partnership</option>
<option value="3">Public Sector</option>
<option value="4">Public Limited Company (Plc)</option>
<option value="5">Charity</option>
</select>
// This should appear if you are a Sole Trader (option 1 in the select box)
<div id="soletrader">
<label for="tax">VAT Number</label>
<input type="text" name="tax" id="tax" />
</div>
// This should appear if you are a Public Sector business (option 3 in the select box)
<div id="publicsector">
<label for="urn">URN Number</label>
<input type="text" name="urn" id="urn" />
</div>
// This should appear if you are a Charity (option 5 in the select box)
<div id="charity">
<label for="charityref">Charity Reference No.</label>
<input type="text" name="charity" id="charityref" />
</div>
用 jquery 编写的 case 语句是解决这个问题最合适的方法吗?我该如何写这样的声明呢?
默认情况下需要隐藏三个输入字段。任何帮助将不胜感激。我对 javascript/jquery 感到沮丧。
What is the most efficient way of showing/hiding form fields based on the value of a select control? I have one select control and three input fields, which show/hide based upon what type of business you work for.
<select id="business-type">
<option value="1">Sole Trader</option>
<option value="2">Partnership</option>
<option value="3">Public Sector</option>
<option value="4">Public Limited Company (Plc)</option>
<option value="5">Charity</option>
</select>
// This should appear if you are a Sole Trader (option 1 in the select box)
<div id="soletrader">
<label for="tax">VAT Number</label>
<input type="text" name="tax" id="tax" />
</div>
// This should appear if you are a Public Sector business (option 3 in the select box)
<div id="publicsector">
<label for="urn">URN Number</label>
<input type="text" name="urn" id="urn" />
</div>
// This should appear if you are a Charity (option 5 in the select box)
<div id="charity">
<label for="charityref">Charity Reference No.</label>
<input type="text" name="charity" id="charityref" />
</div>
Would a case statement - written in jquery - be the most appropriate approach to this matter? How would I write such a statement?
The three input fields need to be hidden by default. Any assistance would be greatly appreciated. I am dismal at javascript/jquery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,您应该向每个字段 div 添加“class='field'”。
然后,使用此代码,您将在下拉列表中选择时显示/隐藏适当的字段:
First, you should add "class='field'" to each field div.
Then, with this code, you will show/hide the appropriate field upon selction in the dropdown:
有几件事:
1)我会向每个附加字段添加一个类(我在下面使用了 .additional-field)。这将允许您一次性隐藏所有它们,而不是要求您显式地
$.hide()
每一个。只有三个还不错,但如果你最终有更多,它会很快变得毛茸茸的。2)我将代码放入一个函数中,并在页面加载时调用该函数并将其用作
.change()
的参数。这样可以避免重复相同的代码,并且涵盖了编辑现有值或需要更正表单中的错误而产生的初始值的情况。否则,除非您选择其他内容然后再次选择该项目,否则附加字段不会显示。Few things:
1) I would add a class (I used .additional-field below) to each of the additional fields. This will allow you to hide them all in one swoop instead of requiring you to
$.hide()
each one explicitly. It's not so bad with just three, but if you ended up with much more, it'd get hairy quick.2) I put the code in a function and both call that function on page load and use it as the parameter to
.change()
. This saves repeating the same code, and covers the case of an initial value from either editing an existing one or an error in the form that needs to be corrected. Otherwise, the additional field wouldn't show until you chose something else and then chose the item again.我认为 if/switch 语句是不必要的。使用更多的 HTML 标记来帮助您的 JavaScript。如果你在每个 div 上放置一个名为“data-val”的属性,并为它分配你希望它一起出现的选项的值,那么 JS 就会变得非常简单。
所以你的 HTML 的 div 看起来像这样:
然后你的 JS 将是:
I don't think an if/switch statement is necessary. Help your JavaScript out a bit with a little more HTML markup. If you put an attribute named "data-val" on each of the divs, and assign it the value of the option you want it to appear along with, then the JS becomes very simple.
So your HTML would look something like this for the divs:
And then your JS would be:
将这些函数放入您的
$.ready(function(){});
Put these functions in your
$.ready(function(){});