如何根据选择框的值更改文本框的值?

发布于 2024-11-05 13:03:31 字数 2079 浏览 3 评论 0原文

我在网上和这个网站上搜索过一个简单且适应性强的解决方案,但遗憾的是,没有任何乐趣。

所需的过程:

  1. 用户从
  2. 只读文本框的值会根据所选<选项>自动填充。 例如与姓名对应的电话号码。
  3. 最后,

基本标记:

当此更改时,更新文本框的值:

<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 所建议的,请参考以下链接:

http://ejohn.org/blog/html-5 -数据属性/

http://blogs.sitepoint.com/rel-not-a-custom-属性/

I've searched online and this website alike for a simple and adaptable solution but alas, no joy.

The desired process:

  1. The user selects an <option> from a <select> list. e.g. a name.
  2. The value of a read-only text box automatically populates based on the selected <option>. e.g. a phone number corresponding to the name.
  3. 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:

http://ejohn.org/blog/html-5-data-attributes/

http://blogs.sitepoint.com/rel-not-a-custom-attribute/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风轻花落早 2024-11-12 13:03:31

在您的选项中添加“数据电话号码”值:

<option value="Elvis" data-phone-number="77777">Elvis</option>

在您的 jquery 中:

$("#name").live("change", function() {
  $("#phonnumber").val($(this).find("option:selected").attr("data-phone-number"));
})

根据需要重构:)

add a "data-phone-number" value on your options:

<option value="Elvis" data-phone-number="77777">Elvis</option>

in your jquery:

$("#name").live("change", function() {
  $("#phonnumber").val($(this).find("option:selected").attr("data-phone-number"));
})

refactor as needed :)

病女 2024-11-12 13:03:31

你可以试试这个。

$('#name').change(function(){
    $('#phonenumber').val($(this).val());
});

如果您希望它显示电话号码,只需将 HTML 设置为将每个选项的值设置为电话号码即可。例如。

<option value="5555555555">Frank</option>

You can try this.

$('#name').change(function(){
    $('#phonenumber').val($(this).val());
});

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.

<option value="5555555555">Frank</option>
最舍不得你 2024-11-12 13:03:31

我会做类似的事情:

<select id="name" name="name">
  <option value="0123456789">Elvis</option>
  [...]
</select>

然后

$('#name').change(function(){
  var thisphone = $(this).val();
  $('#phonenumber').val(thisphone);
});

或者

$('#name').change(function(){
  $('#phonenumber').val($(this).val());
});

取决于你想如何处理你的变量

I would do something like:

<select id="name" name="name">
  <option value="0123456789">Elvis</option>
  [...]
</select>

and then

$('#name').change(function(){
  var thisphone = $(this).val();
  $('#phonenumber').val(thisphone);
});

or

$('#name').change(function(){
  $('#phonenumber').val($(this).val());
});

depending on how you want to work with your variables

我要还你自由 2024-11-12 13:03:31

您也可以在不将选择框和电话号码紧密联系在一起的情况下执行此操作。使用 JSON 表示法来设置保存个人所有信息的对象,例如:

<script type="text/javascript">
    var people = {
        Elvis:  {
            phone: "555-5555"
            },
        Frank: {
            phone: "123-4567"
            },
        Jim: {
            phone: "987-6543"
            }
        };

    window.onload = function() {
        var list = document.getElementById("name");
        list.onchange = function() {
            document.getElementById("phone").value = people[this.value].phone;
        }   
    }
</script>

基本上,我们只是在一个名为“people”的更大对象中创建三个对象“Elvis”、“Frank”和“Jim”。每个对象都有一个phone 属性,您可以使用点符号访问该属性,就像我所做的那样(即people["Elvis"].phone),或数组符号(即people.Elvis["phone"]])。

为了使事情变得简单,我使用“this”关键字,因为当从“onchange”事件处理程序中调用时,它指的是触发该事件的对象 - 选择框。所以“this.value”指的是选择框上的值。

现在您的选择仍然非常简单:

<select id="name" name="name">
    <option value="Elvis">Elvis</option>
    <option value="Frank">Frank</option>
    <option value="Jim">Jim</option>
</select>

<br />
<input type="text" id="phone" />

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:

<script type="text/javascript">
    var people = {
        Elvis:  {
            phone: "555-5555"
            },
        Frank: {
            phone: "123-4567"
            },
        Jim: {
            phone: "987-6543"
            }
        };

    window.onload = function() {
        var list = document.getElementById("name");
        list.onchange = function() {
            document.getElementById("phone").value = people[this.value].phone;
        }   
    }
</script>

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:

<select id="name" name="name">
    <option value="Elvis">Elvis</option>
    <option value="Frank">Frank</option>
    <option value="Jim">Jim</option>
</select>

<br />
<input type="text" id="phone" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文