简体   繁体   English

如何使用JQuery在更改时立即填写文本框?

[英]How to instantly fill in a text box on change using JQuery?

I have a textbox which fills in the city. 我有一个填满城市的文本框。 I have an autocomplete feature for that and its the standard autocomplete feature with data coming in from the db. 我有一个自动完成功能,它具有标准的自动完成功能,数据来自数据库。

The data coming in has city, state and country information.When the user selects the city from the autocomplete dropdown, it should populate the city, state and country. 输入的数据包含城市,州和国家/地区信息,当用户从自动完成下拉列表中选择城市时,应填充城市,州和国家/地区。 So I have an onchange on the city for this. 因此,我为此在城市上班了。 The problem is that the state and country are filled only when I click on the respective textboxes and not instantly as it happens with on change. 问题在于,仅当我单击相应的文本框时才填充州和国家,而不是在更改时立即填充。 Code: 码:

$(".find-city").autocomplete(cityConfirmAutoCompleteOption);
$(".find-city").autocomplete( "option", "appendTo", "#"+'button'+q.id );

$(".find-city").change(function(city) {
$(".state").val(statecode);
}); 

HTML: HTML:

<div class="find-city"  style="float: left;">
<table>             
<tbody><tr><td><label>City</label></td>
    <td><input type="text" class="find-city"></td></tr>
    </tbody></table>
</div> 

<div class="state"  style="float: left;">
<table>             
<tbody><tr><td><label>State</label></td>
    <td><input type="text" class="state"></td></tr>
    </tbody></table>

Without seeing any of your HTML and most of your JavaScript, Could you not just do this: 在没有看到任何HTML和大部分JavaScript的情况下,您能否仅执行以下操作:

$(".find-city").change(function(city) {
    $(".state").val(statecode);
    $(".city").val(citycode);
    //... etc.
}); 

If you are having problem trying to set the value of the input box, try this: 如果您在尝试设置输入框的值时遇到问题,请尝试以下操作:

$('.state').attr('value',stateCode);

You can see similar demo here: http://jsfiddle.net/Lwhw4sj9/ 您可以在此处看到类似的演示: http : //jsfiddle.net/Lwhw4sj9/

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

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