简体   繁体   English

在jQuery中使用json值更新表单字段的值

[英]update value of a form field with json values in jQuery

I have a dropdown firm filed with countries like "Belgium", "Barbados" etc with id = " country " and another text input with phone number to be filled with id = " tele ". 我有一家下拉式公司,向“比利时”,“巴巴多斯”等国家/地区提交了ID =“ 国家/地区 ”,另一个文本输入的电话号码由ID =“ Tele ”填充。 I need the country calling code to be updated in the tele id input filed based on country dropdown. 我需要根据国家/地区下拉列表在电话ID输入中更新国家/地区电话代码。 I have a json file of country calling code mapped to country names in format ( countrycode.json ): 我有一个国家/地区调用代码的json文件,以格式( countrycode.json )映射到国家/地区名称:

{
  "countries": [
    {
      "code": "+7 840",
      "name": "Abkhazia"
    },
    {
      "code": "+93",
      "name": "Afghanistan"
    },

etc.... 等等....

My current jQuery code is as follows: 我当前的jQuery代码如下:

<script>
$('#country').change(function() {
    var countrycode = $(this).val();
    $('#tele').val(countrycode);
});
</script>

which will update the tele id with value from country id. 它将使用国家/地区ID中的值更新电视ID。

I need the country calling code to be selected from the json file based on the country chosen. 我需要根据所选国家/地区从json文件中选择国家/地区调用代码。 How is it possible? 这怎么可能?

Thanks in advance. 提前致谢。

The tags on your question are "jquery/jason". 您问题上的标签是“ jquery / jason”。 Using only those tools, you will have to embed the file into your html, probably as an object of key/value pairs. 仅使用那些工具,您将不得不将文件嵌入到html中,可能作为键/值对的对象。

How can I add a key/value pair to a JavaScript object? 如何将键/值对添加到JavaScript对象?

If you don't want to embed it and have your own server, you can make ajax calls to the server to request the info you want. 如果您不想嵌入它并拥有自己的服务器,则可以对服务器进行ajax调用以请求所需的信息。 But you'll have to use your favorite server side scripting language (php, python, ruby, etc...) to send the response, then update the response value with jquery. 但是您必须使用自己喜欢的服务器端脚本语言(php,python,ruby等)来发送响应,然后使用jquery更新响应值。

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

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