[英]Populating a form from a specific <li> tag on click
I have a program that takes a users input and filters an API and returns the results in the form of a <li>
tag. 我有一个程序,它接受用户输入并过滤API,并以
<li>
标记的形式返回结果。 The results from the API can be greater than 1,000 so I wanted the user to be able to click on a particular result and have that populate a <form>
. API的结果可能大于1,000,因此我希望用户能够单击特定结果并填充
<form>
。
Everything works fine except that if I have multiple results, then try and click on a specific <li>
, my input
tags populate with every value from every <li>
on the page. 一切正常,除了如果我有多个结果,然后尝试单击特定的
<li>
,我的input
标签将填充页面上每个 <li>
每个值。 What am I missing? 我想念什么?
This is the <li>
. 这是
<li>
。
`
<li class="populate">
<span class="name" data-key="name">${place.Name}</span>
</br>
<span class="address" data-key="address">${place.Address}</span> <span class="city" data-key="city">${place.City}</span>
</br>
<span class="state" data-key="state">${place.StateProvince}</span>
<span class="postal-code" datakey="postal-code">${place.PostalCode}</span>
</br>
<span class="country" data-key="country">${place.Country}</span>
</br>
<span class="dot-code" data-key="dot-code"><span class="highlight">${dotCode}</span></span>
</li>
`
This is the html
where my <li>
tags go. 这是我的
<li>
标记所在的html
。
<form class="search-form">
<input type="text" class="search" placeholder="Plant name">
<ul class="suggestions">
<li>Results will show here</li>
</ul>
This is the <form>
I am populating. 这是我正在填充的
<form>
。
<form class="upload-form">
<input type="text" class="form-two f_code" placeholder="DOT Code" name="dot-code">
<input type="text" class="form-two f_plant" placeholder="Plant" name="name">
<input type="text" class="form-two f-add1" placeholder="Address" name="address">
<input type="text" class="form-two f_add4" placeholder="State" name="state">
<input type="text" class="form-two f_city" placeholder="City" name="city">
<input type="text" class="form-two f_zip" placeholder="Zip Code" name="postal-code">
<input type="text" class="form-two f_cntry" placeholder="Country" name="country">
</form>
Finally, this is where I populate the <form>
. 最后,这是我填充
<form>
。 After referencing the jQuery documentation, I thought .index()
was a possible solution, however I'm not quite sure how to implement it, or if this is even an applicable scenario. 在引用jQuery文档之后,我认为
.index()
是一个可能的解决方案,但是我不太确定如何实现它,或者即使这是适用的情况。 I tried using .index()
after .text()
but I know this isn't valid. 我尝试在
.text()
.index()
之后使用.index()
,但我知道这是无效的。
$('.suggestions li').click(function() {
$('.upload-form').children().each(function() {
let key = $(this).attr('name');
let txt = $('.populate span[data-key="'+ key +'"]').text();
$(this).val(txt);
});
});
Some disclaimers: I've been programming for just over a year now and I've been programming professionally for around 2 months so I apologize if I'm missing some minor detail. 一些免责声明:我已经编程了一年多,而我已经从事专业编程大约2个月了,因此,如果我遗漏了一些小细节,我深表歉意。
$('.suggestions li').click(function() {
//keep track of the li that was clicked
var $li = $(this);
$('.upload-form').children().each(function() {
//this was doing a global lookup on the populate.
//don't do this. you already have your specific li
//let txt = $('.populate span[data-key="'+ key +'"]').text();
let txt = $li.find('span[data-key="'+ this.name +'"]').text();
//or since the spans class matches the data-key you could use that
let txt = $li.find('.'+ this.name).text();
this.value = txt;
});
});
The part you are missing is the $(this) for the li that you clicked. 您缺少的部分是您单击的li的$(this)。 You could try something like this.
您可以尝试这样的事情。
$('.suggestions li').click(function() {
$(this).children('span').each(function() {
let key = $(this).attr('data-key');
let txt = $(this).text();
$('.upload-form[name="' + key + '"]').val(txt);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.