如何根据选择框的值更改文本框的值?
我在网上和这个网站上搜索过一个简单且适应性强的解决方案,但遗憾的是,没有任何乐趣。
所需的过程:
- 用户从
- 只读文本框的值会根据所选
<选项>
自动填充。 例如与姓名对应的电话号码。 - 最后,
基本标记:
当此更改时,更新文本框的值:
<select id="name" name="name">
<option value="Elvis">Elvis</option>
<option value="Frank">Frank</option>
<option value="Jim">Jim</option>
</select>
值取决于上述框的值:
<input type="text" id="phonenumber" name="phonenumber" value="">
我的问题:
如何实现此目的使用 jQuery 还是常规 Javascript?请从客户端新手的角度来回答这个问题。
解决方案(感谢 Corroded):
我将以下内容添加到文档的头部:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#name").live("change", function() {
$("#phonenumber").val($(this).find("option:selected").attr("data-phonenumber"));
})
});
</script>
...并将以下内容添加到文档的正文:
<select id="name" name="name">
<option value="" selected="selected">Please select...</option>
<option value="Elvis" data-phonenumber="11111">Elvis</option>
<option value="Frank" data-phonenumber="22222">Frank</option>
<option value="Jim" data-phonenumber="33333">Jim</option>
</select>
<input type="text" id="phonenumber" name="phonenumber" value="" readonly="readonly">
有关使用 data-somename 作为属性的更多信息,如 corroded 所建议的,请参考以下链接:
I've searched online and this website alike for a simple and adaptable solution but alas, no joy.
The desired process:
- The user selects an
<option>
from a<select>
list. e.g. a name. - The value of a read-only text box automatically populates based on the selected
<option>
. e.g. a phone number corresponding to the name. - Finally, both the
<select>
box and text box must each have their own values. i.e. a name and a phone number respectively.
The basic mark up:
When this changes, update the value of my text box:
<select id="name" name="name">
<option value="Elvis">Elvis</option>
<option value="Frank">Frank</option>
<option value="Jim">Jim</option>
</select>
Value contingent on the value of above box:
<input type="text" id="phonenumber" name="phonenumber" value="">
My question:
How do I achieve this using jQuery or regular Javascript? Please answer this from the perspective of a client side novice.
The solution (thanks to corroded):
I added the following to the head of my document:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#name").live("change", function() {
$("#phonenumber").val($(this).find("option:selected").attr("data-phonenumber"));
})
});
</script>
...and the following to the body of my document:
<select id="name" name="name">
<option value="" selected="selected">Please select...</option>
<option value="Elvis" data-phonenumber="11111">Elvis</option>
<option value="Frank" data-phonenumber="22222">Frank</option>
<option value="Jim" data-phonenumber="33333">Jim</option>
</select>
<input type="text" id="phonenumber" name="phonenumber" value="" readonly="readonly">
For more information on the use of data-somename as an attribute, as suggested by corroded, refer to the following links:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的选项中添加“数据电话号码”值:
在您的 jquery 中:
根据需要重构:)
add a "data-phone-number" value on your options:
in your jquery:
refactor as needed :)
你可以试试这个。
如果您希望它显示电话号码,只需将 HTML 设置为将每个选项的值设置为电话号码即可。例如。
You can try this.
If you would like it to display the phone number just set up your HTML to have the value of each option be the phone number. For instance.
我会做类似的事情:
然后
或者
取决于你想如何处理你的变量
I would do something like:
and then
or
depending on how you want to work with your variables
您也可以在不将选择框和电话号码紧密联系在一起的情况下执行此操作。使用 JSON 表示法来设置保存个人所有信息的对象,例如:
基本上,我们只是在一个名为“people”的更大对象中创建三个对象“Elvis”、“Frank”和“Jim”。每个对象都有一个phone 属性,您可以使用点符号访问该属性,就像我所做的那样(即people["Elvis"].phone),或数组符号(即people.Elvis["phone"]])。
为了使事情变得简单,我使用“this”关键字,因为当从“onchange”事件处理程序中调用时,它指的是触发该事件的对象 - 选择框。所以“this.value”指的是选择框上的值。
现在您的选择仍然非常简单:
You can also do this without tying the select box and the phone numbers so tightly together. Use JSON notation to set up objects that hold all of your person's info, such as:
Basically, we're just creating three objects, "Elvis", "Frank" and "Jim" inside a bigger object called "people". Each object has a phone property that you can access with the dot notation, as I've done (i.e. people["Elvis"].phone), or the array notation (i.e. people.Elvis["phone"]]).
To make things easy, I am using the "this" keyword since when called from within the "onchange" event handler, it refers to the object that fired the event -- the select box. So "this.value" refers to the value on the select box.
Now your select stays pretty simple: