gpt4 book ai didi

javascript - jquery 选择器中包含+startswith

转载 作者:行者123 更新时间:2023-11-28 21:00:32 25 4
gpt4 key购买 nike

我需要根据标签文本值的第一个字母来选择元素。例如:

<li><label for="1"><input type="checkbox" name="contacts" value="1">Aaron</label></li>
<li><label for="2"><input type="checkbox" name="contacts" value="2">Born</label></li>
<li><label for="3"><input type="checkbox" name="contacts" value="3">Bale</label></li>
<li><label for="4"><input type="checkbox" name="contacts" value="4">Kate</label></li>

我想选择文本值以 B 开头的所有输入 - 结果应该是 Born 和 Bale 输入。我怎样才能使用 jQuery/纯 javascript 做到这一点?

最佳答案

试试这个:http://jsfiddle.net/bb2uE/

HTML:

<label for="1">Aaron</label><input type="checkbox" name="contacts" value="1"></li>
<li><label for="2">Born</label><input type="checkbox" name="contacts" value="2"></li>
<li><label for="3">Bale</label><input type="checkbox" name="contacts" value="3"></li>
<li><label for="4">Kate</label><input type="checkbox" name="contacts" value="4"></li>

​Javascript:

$(document).ready(function(){
var obj = [];
$.each($('label'), function(){
if ($(this).html().charAt(0) == 'B')
obj.push($(this).html());
});
for (var i = 0; i < obj.length; i++)
console.log(obj[i]);
});

或者,如果您想隐藏这些输入:http://jsfiddle.net/bb2uE/1/

Javascript:

var hideFunction = function(letter){
$.each($('label'), function(){
if ($(this).html().charAt(0) == letter)
$(this).parent().hide();
});
}
hideFunction('B');

关于javascript - jquery 选择器中包含+startswith,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11163179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com