jQuery从对象获取元素-国际电话输入从国家/地区数据获取拨号代码 [英] jquery get elements from object - intl-tel-input get dial code from country data

查看:247
本文介绍了jQuery从对象获取元素-国际电话输入从国家/地区数据获取拨号代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jquery的新手,我正在尝试在提交表单后从intl-tel-input中的SelectedCountryData对象获取值.

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

从文档getSelectedCountryData返回类似下面的内容,我想从中获取值"93":

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

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

我可以成功地返回电话号码,但是当我使用以下命令时,会返回[calling_code] => [object Object]作为呼叫代码:

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
});

以下是表单输入字段:

<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">

推荐答案

我设法解决了这个问题,希望对其他人有用:

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

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

这篇关于jQuery从对象获取元素-国际电话输入从国家/地区数据获取拨号代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆