简体   繁体   English

获取 HTML class 的值,然后放入一个输入元素

[英]Get the value of HTML class, then put in in an input element

I've been trying to get the value of the ip address, which is already inside <p id='details'></p> to be put in the value of the input html class as well found on the last line.我一直在尝试获取 ip 地址的值,该地址已经在<p id='details'></p>中,以便放入输入 html ZA2F2ED4F8EBC2CBB14C21A29DC40 上的值中。

I tried declaring it as a php variable but I couldn't afford so.我尝试将其声明为 php 变量,但我负担不起。

<script>
    var ipinfo;
    $.getJSON("https://ipinfo.io", function (data) {
    $("#details").html(data.ip)
    })
</script>
    
<p id='details'></p>
    
<input type="text" name="ip" class="" value="@php echo ""; @endphp" readonly>

You can firstly get hold of the content of a particular HTML class by copying over the value into a variable, and then inject this value by assigning the copied over variable's value into the input's value.您可以首先通过将值复制到变量中来获取特定 HTML class 的内容,然后通过将复制的变量值分配给输入值来注入该值。 You could achieve this by using plain Vanilla JavaScript.您可以通过使用普通的 Vanilla JavaScript 来实现这一点。

Since your p tag is initially empty and the data is injected into the p tag after it is fetched from a network, this process becomes an asynchronous task.由于您的p标签最初是空的,并且在从网络获取数据后将数据注入p标签,因此该过程成为异步任务。 So, you'd want to feed the data only after it is fetched, so you can add the value assigning logic within the fetch function as follows:因此,您只想在获取数据后才提供数据,因此您可以在 fetch function 中添加值分配逻辑,如下所示:

    var ipinfo;
    $.getJSON("https://ipinfo.io", function (data) {
      document.querySelector("#details").innerHTML = data.ip

      const inject = document.querySelectorAll("input[type=text]")[0] //Identify the input element

      inject.value = data.ip //Assign the returned value to input's value.
    })

As you want the returned value from fetch to be fed to the p tag content as well as the input tag value, you could directly assign it, instead of copying over the already copied value.由于您希望将 fetch 的返回值馈送到p标签内容以及输入标签值,因此您可以直接分配它,而不是复制已经复制的值。

From what i Can see you just need a token.从我可以看到你只需要一个令牌。

Also, I've used ES6 fetch method to get the API object response另外,我使用 ES6 获取方法来获取 API object 响应

 fetch('https://ipinfo.io/json?token=e0b12a7d02537a').then( (response) => response.json() ).then( (jsonResponse) => { //this will display the IP as ap elemenet text $('#details').html('IP Address = ' + jsonResponse.ip) //here we are assigning it as a value to the input $('input[name="ip"]').val(jsonResponse.ip); } )
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id='details'></p> <input type="text" name="ip" class="" readonly>

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

相关问题 如何获取被单击元素的类值并使用此值在jQuery中此类的最后一次出现之后放置一段html代码 - How to get class value of a clicked element and use this value to put a piece of html code after the last occurance of this class in jquery 我如何让我的 html 输入元素影响我的 javascript 类对象值(字符名称) - How do i get my html input element to influence my javascript class object value(character name) 将元素属性放入输入值 - Put element attribute to input value 从td获取转义的html并将其作为值输入 - Get escaped html from td and put it into input as value 获取跨度的每个类的值并将字符串输入 - get value of each class of span and put string to input 如何通过单击另一个类元素获取类的当前html值 - How to get current html value of a class with click on another class element 当我以编程方式输入值时,HTML输入元素无法识别值 - HTML input element does not recognize value when I put value programmatically 如何使用jquery类选择器获取HTML Input Element标签? - How to get HTML Input Element tag with jquery class selector? Typescript:通过使用 html 元素 ID 获取输入值,Object 可能为“空” - Typescript: Object is possibly 'null' by get the input value with html element id 如何获取输入值以添加为 jquery 中当前选定元素的类 - How get input value to add as class for current selected element in jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM