jquery get elements from object - intl-tel-input get dial code from country data

こ雲淡風輕ζ 提交于 2019-12-12 04:24:13

问题


I am new to jquery and I'm trying to get the values from the SelectedCountryData object in intl-tel-input after form submission.

From the documentation getSelectedCountryData returns something like below and I want to get the value "93" from it:

{
  name: "Afghanistan (‫افغانستان‬‎)",
  iso2: "af",
  dialCode: "93"
}

I can successfully return the phone number but [calling_code] => [object Object] is returned for the calling code when I use the following:

$("form").submit(function() {
     $("#phone-full").val($("#phone").intlTelInput("getNumber")); //this works
     $("#calling_code").val($("#phone").intlTelInput("getSelectedCountryData")); //this doesn't
});

Here are the form input fields:

<input type="tel" id="phone" value="<?php echo $phone_number ?>">
<input id="phone-full" type="hidden" name="phone_number">
<input id="calling_code" type="hidden" name="calling_code">

回答1:


I managed to solve this so in the hope that it is useful for someone else:

$("#calling_code").val($("#phone").intlTelInput("getSelectedCountryData").dialCode);


来源:https://stackoverflow.com/questions/42699022/jquery-get-elements-from-object-intl-tel-input-get-dial-code-from-country-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!