简体   繁体   English

使用数组从下拉列表获取文本字段值时遇到问题

[英]Trouble getting text field value from a dropdown, using an array

I am trying to change a text field value for a phone number based on what is selected in a drop down for the user's country. 我正在尝试根据用户所在国家/地区的下拉列表中选择的内容来更改电话号码的文本字段值。

The user should select their country and then either before or after they've typed in their phone number I need the country code to be populated into the phone number text box. 用户应该选择他们的国家,然后在输入他们的电话号码之前或之后,我需要将国家/地区代码填充到电话号码文本框中。

Here is what I have so far, if anyone has any solutions or ways I can get this working I would be very grateful. 到目前为止,这里就是我所拥有的,如果有人有任何解决方案或方法可以使我工作,我将非常感激。

NOTE: I have waaay more country codes than listed, but you'll get an idea of the setup with my including just a few. 注意:我的国家/地区代码比列出的要多,但是您将通过其中的一些设置了解一下设置。

Thanks! 谢谢!

var phoneNum = jQuery('#l_tel').val();
jQuery(function() {
console.log(phoneNum);
var selectValues = {
"AF" : "93 " + phoneNum,
"AX" : "358 " + phoneNum,
"AL" : "355 " + phoneNum,
"DZ" : "213 " + phoneNum,
"AS" : "1 684 " + phoneNum,
"AD" : "376 " + phoneNum,
"AO" : "244 " + phoneNum,
"AI" : "1 264 " + phoneNum,
};

var jPhone = jQuery('#l_tel').val();
var jCountry = jQuery('select.country');
jCountry.change(function() {
    phoneNum = jQuery('#l_tel').val();
    jPhone.empty().append(function() {

    var output = '';
        jQuery.each(selectValues[jCountry.val()], function(key, value) {
            output += key;
        });
        return output;
            });
}).change();
jQuery('select.country').val('');
});

Basically you have take the current number jQuery('#l_tel').val() and add the country number selectValues[jCountry.val()] to it. 基本上,您使用当前数字jQuery('#l_tel').val()并向其添加国家/地区selectValues[jCountry.val()] Then use jQuery('#l_tel').val(...) to set the new number. 然后使用jQuery('#l_tel').val(...)设置新数字。

You have also to remember the previous selected country number to remove it from the phoneNum because otherwise you would end up with the old country number AND the new selected country number. 您还必须记住先前选择的国家/地区编号,以将其从phoneNum删除,否则您将得到原来的国家/地区编号和新的国家/地区编号。

var countryNum;
var jPhone = jQuery('#l_tel');
var jCountry = jQuery('select.country');
jCountry.change(function () {
    var phoneNum = jPhone.val();

    // remove the previous selected country number
    // ^ means that it should only be replaced if the string starts with this number
    if (countryNum) {
        phoneNum = phoneNum.replace(new RegExp("^" + countryNum), "");
    }

    countryNum = selectValues[jCountry.val()];
    var newPhoneNum = countryNum + phoneNum;
    jPhone.val(newPhoneNum);
}).change(); 

note: you have to use jPhone.val() to set value of the text field. 注意:您必须使用jPhone.val()设置文本字段的值。

working example: http://jsfiddle.net/uLcxqvxo/ 工作示例: http : //jsfiddle.net/uLcxqvxo/

Can be a good start : 可以是一个好的开始:

HTML : HTML:

<select id="countries">
    <option value="" selected>Choose country...</option> 
    <option value="93">AF</option> 
    <option value="358">AX</option>
    <option value="355">AL</option>
    <option value="213">DZ</option>
    <option value="1 684">AS</option>
    <option value="376">AD</option>
    <option value="244">AO</option>
    <option value="1 264">AI</option>
</select>
<input id="tel" value="" />

JAVASCRIPT : JAVASCRIPT:

$(function() {

    var jSel = $('#countries');
    var jTel = $('#tel');
    var separator = '-';
    var lastPrefix = '';

    // Bind on change event
    jSel.on('change', function() {

        var prefix = '';
        var option = $(this).find(":selected").val();

        if (option != '') {
            prefix = option + separator;
        }

        // get Tel
        var currentTel = jTel.val();

        // check if the tel already contains the separator
        var r = new RegExp(separator,"g");
        if(lastPrefix != '' && currentTel.match(r) != null) {
            currentTel = currentTel.split(separator);
            currentTel.shift();
            currentTel = currentTel.join(separator);
        }

        jTel.val(prefix + currentTel);

        lastPrefix = prefix;
    });

});

Working example on JsFiddle : http://jsfiddle.net/z8fe5ovc/2/ JsFiddle上的工作示例:http://jsfiddle.net/z8fe5ovc/2/

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

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