简体   繁体   English

如何在输入标签中传递值?

[英]How to pass value in input tag?

normally, i am trying passing value in p,div and pre tag and values are show in above tag now i am passing country name in input tag. 通常,我正在尝试在p,div和pre标签中传递值,并且现在在上面的标签中显示值,而我在输入标签中传递国家名称。 but value are not show in input tag. 但值不会显示在输入标签中。 Here is my code: 这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Get browser and ip address </title>
</head>
<body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1><a href="https://ipdata.co">ipdata.co</a> - IP geolocation API</h1>
<input type="text" name="country_name" id="country_name" value="">
<p id="country_name"></p>
<div id="ip"></div>
<div id="city"></div>
<pre id="response"></pre>

<script> 

$.get("https://api.ipdata.co?api-key=test", function (response) {
    $("#ip").html("IP: " + response.ip);
    $("#city").html(response.country_name + ", " + response.region);
    $("#country_name").html(response.country_name);
    $("#response").html(JSON.stringify(response, null, 4));
}, "jsonp");

</script>

</body>
</html>

Thanks. 谢谢。

Only one item can have a specific id. 只有一项可以具有特定ID。 Try using classes if you want them to share something. 如果您希望他们共享某些东西,请尝试使用它们。 Since both the 由于两者

and have the same id it will not work Note that you can getElement S ByClassName but you can only get Element ById with the class allowing multiple and ID only allowing for one 并具有相同的ID它不会工作,请注意,您可以getElement 小号 ByClassName但你只能与类允许多个和ID只允许一个获得元素 ById

input fields take value attribute. 输入字段具有value属性。 Try changing this line: $("#country_name").val(response.country_name); 尝试更改此行: $("#country_name").val(response.country_name);

Also, like @Khaleb said, element IDs must be unique. 另外,就像@Khaleb所说的那样,元素ID必须是唯一的。

You are trying to set input value using .html() . 您正在尝试使用.html()设置输入值。 Which is not supported in jquery. jQuery不支持该功能。 You need to use .val() to set input value. 您需要使用.val()来设置输入值。 Here is the explanation for the same. 是相同的解释。

Try this: 尝试这个:

 <!DOCTYPE html> <html> <head> <title>Get browser and ip address </title> </head> <body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1><a href="https://ipdata.co">ipdata.co</a> - IP geolocation API</h1> <input type="text" name="country_name" id="country_name" value=""> <p id="country_name"></p> <div id="ip"></div> <div id="city"></div> <pre id="response"></pre> <script> $.get("https://api.ipdata.co?api-key=test", function (response) { $("#ip").html("IP: " + response.ip); $("#city").html(response.country_name + ", " + response.region); $("#country_name").val(response.country_name); $("#response").html(JSON.stringify(response, null, 4)); }, "jsonp"); </script> </body> </html> 

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

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