jQuery获取选中复选框的标签 [英] jQuery getting label for checked checkbox
问题描述
在下面的代码中,当我检查"Mexico"时,我不断得到"MexicoMexico"作为标签文本的返回.对于所有其他字段,我没有得到这个重复的结果,仅适用于该字段.在第一次分配countryvalues [i]之后,立即发生了这个问题,我不知道为什么.
In my below code, I keep getting "MexicoMexico" returned as the label text when I check "Mexico". For all the other fields I am not getting this duplicate result, it is only for this one field. The issue occurs immediately after the first assignment of countryvalues[i] and I don't see why.
<div id="country">
....
<li><input type="checkbox" name="country" value="mexico" class="checkbox">
<label for="mexico">Mexico</label></input></li>
</div>
countryvalues = $('#country input:checkbox:checked').map(function() {
return this.value;
}).get();
for (var i=0; i<countryvalues.length; i++)
{
countryvalues[i] = $("label[for='" + countryvalues[i] + "']").text();
countryvalues[i] = countryvalues[i].split(' ').join('%20');
fields = fields + "coveraa!";
url = url + countryvalues[i] + "!";
}
推荐答案
Sarina,我只能建议您尝试获取在.map()
回调中查找的字符串,而不是再次使用for(...){...}
循环.
Sarina, I can only suggest you try obtaining the string you seek within the .map()
callback rather than looping again with for(...){...}
.
var countryvalues = $('#country input:checkbox:checked').map(function() {
return $(this).next("label").text().split(' ').join('%20');
}).get();
确保清除了</input>
的所有实例的HTML,并确保输入元素是自动关闭的<input ... />
.
Make sure the HTML is purged of all instances of </input>
and that the input elements are self-closed, <input ... />
.
这篇关于jQuery获取选中复选框的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!