Javascript 从特定表单填充表单<;李>;点击标签
我有一个程序,它接受用户输入,过滤API,并以Javascript 从特定表单填充表单<;李>;点击标签,javascript,jquery,html,Javascript,Jquery,Html,我有一个程序,它接受用户输入,过滤API,并以标记的形式返回结果。API的结果可能大于1000,因此我希望用户能够单击特定的结果,并使其填充 一切正常,但如果我有多个结果,然后试着点击一个特定的,我的输入标记将填充页面上每个的每个值。我错过了什么 这是 ` <li class="populate"> <span class="name" data-key="name">${place.Name}</span> </br>
标记的形式返回结果。API的结果可能大于1000,因此我希望用户能够单击特定的结果,并使其填充
一切正常,但如果我有多个结果,然后试着点击一个特定的
,我的输入
标记将填充页面上每个
的每个值。我错过了什么
这是
`
<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>
`
这是我正在填充的
<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>
一些免责声明:我已经编程一年多了,我已经专业编程了大约2个月,所以如果我遗漏了一些小细节,我道歉 您缺少的部分是您单击的li的$(this)。你可以试试这样的
$('.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;
});
});
$('.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);
});
});
您缺少的部分是您单击的li的$(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);
});
});
谢谢你的帮助。我从他的问题中抄袭了这一点,并在研究过程中对其进行了更新。我已经将其更新为使用我们目前所处跨度的$(this)。感谢您的帮助。我从他的问题中抄袭了这一点,并在研究过程中对其进行了更新。我已将其更新为使用我们当前所在跨度的$(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);
});
});