简体   繁体   English

每次用户进行选择时,如何使用国家/地区下拉列表的值填充文本框?

[英]How to populate a textbox with value of country dropdown each time user makes a selection?

I have an issue related to an HTML page. 我有一个与HTML页面有关的问题。 Hoping that someone could help resolve it - 希望有人可以帮助解决它-

I have an HTML page with a Country select and a Text Box. 我有一个带有国家(地区)选择和文本框的HTML页面。 The value attribute of the Option tag contains the Country codes. Option标签的value属性包含国家(地区)代码。 When the User selects a country, its respective country code should get populated in the Text Box. 当用户选择一个国家时,其各自的国家代码应填充在文本框中。 This will happen each time the User changes the country. 用户每次更改国家/地区都会发生这种情况。 All of the data is present in the page itself, and no server calls are made to fetch the data. 所有数据都存在于页面本身中,并且不进行任何服务器调用来提取数据。

Could this be done using the delegate method? 可以使用委托方法来完成吗? Can I also use Ajax in this case? 在这种情况下,我也可以使用Ajax吗?

Any help would be very much appreciated. 任何帮助将不胜感激。

Many thanks in advance!! 提前谢谢了!!

By using Jquery 通过使用jQuery

Try this 尝试这个

HTML HTML

<select id="c_code">
    <option value="IN">India</option>
    <option value="US">USA</option>
</select>
<input id="code" type="text">

Script 脚本

$('#c_code').on('change',function(){
   $('#code').val($(this).val());
});

DEMO DEMO

You simply need to bind the logic to the change event of select 您只需要将逻辑绑定到select的change事件即可

Here is the html code 这是HTML代码

    <select id="ddl">
    <option value="001">India</option>
    <option value="002">USA</option>
</select>

<input type="text" id="txt" />

Here is the jQuery 这是jQuery

    $('#ddl').change(function(){
    $('#txt').val($('#ddl').val());
});

Here is the demo 这是演示

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM