简体   繁体   中英

Insert country code to phone input field with jQuery

In a "contact us" form, I need to insert a country code (ie 972, 1, 91) to the phone input field after choosing the country from the select list. For example; If a user chooses "US" from the list, than the phone input field will start with the number "1" and than the user will continue to write his number. How do I do that with jQuery?

Thank you

You can do this buy adding the value as code of the Country like for US the value of option should be 1

Your HTML should be,

<select id="country">
   <option value="">Select Country</option>
   <option value="1">US</select>
   <option value="91">IN</select>
</select><br/>
Code: <input type="text" id="code" />

Jquery

$(function(){
   $('#country').on('change',function(){
       $('#code').val(this.value);// changing the code textbox value by current country value
   });
});

HTML:

<select id="country">
    <option value="">Select Country</option>
    <option value="1">US</option>
    <option value="+44 ">UK</option>
</select>
<input id="phone" type="text" name="phone">

jQuery:

$(function() {
    $('#country').on('change', function() {
        $('#phone').val($(this).val());
    });
});
<select id="country">
   <option value="">Select Country</option>
   <option value="1">US</option>
   <option value="91">IN</option>
   </select>
<br><br>
Code: <input type="text" id="code" />

$(function() {
    $('#country').on('change', function() {
        $('#phone').val($(this).val());
    });
});`enter code here`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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