我想将文本与动态复选框匹配。这意味着如果我在文本框中写入 H,那么它将显示其中包含 H 的所有复选框。这是我的适用于静态代码的代码但不适用于动态请帮助我,我是 jquery 和 java 脚本的新手
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset="utf-8" />
<script type="text/javascript"> $(function () {
$('#chkfilter').on('keyup', function () {
var query = this.value;
$('[id^="chk"]').each(function (i, elem) {
if (elem.value.indexOf(query) != -1) {
$(this).closest('label').show();
} else {
$(this).closest('label').hide();
}
});
});
});</script>
<style>
input
{
display: inline;
}
</style>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<label>
<input type="checkbox" name="chk" id="chk1" value="casual">casual</label><br>
<label>
<input type="checkbox" name="chk" id="chk2" value="intermediate">intermediate</label><br>
<label>
<input type="checkbox" name="chk" id="chk3" value="hunter">hunter</label><br>
<label>
<input type="checkbox" name="chk" id="chk4" value="forest">forest</label><br>
<label>
<input type="checkbox" name="chk" id="chk5" value="term">extreme</label><br>
<label>
<input type="checkbox" name="chk" id="chk6" value="river">river</label><br>
<label>
<input type="checkbox" name="chk" id="chk7" value="beach">beach</label><br>
<br>
<br>
<input type="text" id="chkfilter">
静态代码
当我将其转换为动态复选框时,将显示其值,但当我尝试匹配它时,它不起作用
<小时/>动态代码
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset="utf-8" />
<script type="text/javascript">
$(function () {
$('#chkfilter').on('keyup', function () {
var query = this.value;
$('[id^="list"]').each(function (i, elem) {
if (elem.value.indexOf(query) != -1) {
$(this).closest('label').show();
} else {
$(this).closest('label').hide();
}
});
});
});
</script>
<style>
input
{
display: inline;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<label>
<ul id="list" runat="server" style="list-style-type: none;">
</ul>
</label>
<div id="filters" runat="server">
</div>
<input type="text" id="chkfilter">
</div>
protected void Page_Load(object sender, EventArgs e)
{
fillcolor();
}
public void fillcolor()
{
string str = string.Empty;
string str1 = string.Empty;
string result = string.Empty;
clsColor objcolor = new clsColor(true);
clsColorProduct objProduct = new clsColorProduct(true);
objcolor.getColor();
for (int i = 0; i < objcolor.ListclsColor.Count; i++)
{
str += "<li><div class='filterblock'><input type='checkbox' id=" + objcolor.ListclsColor[i].ColorID + " name='chk' value=" + objcolor.ListclsColor[i].ColorName + " class='category'><div class='font'><div class='Color' style=background-color:" + objcolor.ListclsColor[i].ColorCode + "></div></div><label class='font' for=" + objcolor.ListclsColor[i].ColorID + ">" + objcolor.ListclsColor[i].ColorName + "</label></div></li>";
if (objcolor.ListclsColor[i].ColorID != 0)
{
str1 += "<td><div class='resultblock' ColorID=" + objcolor.ListclsColor[i].ColorID + " data-tag=" + objcolor.ListclsColor[i].ColorName + "><div class='desc'><div class='desc_text'><div class='main'><div class='sub'><div class='box'>" + objcolor.ListclsColor[i].ColorName + "<span class=''><img height='10px' style='margin-left: 4px;margin: 0px 0px -1px 4px;'/></span></div></div></div></div></div></div></td>";
objProduct.getColorp();
for (int j = 0; j < objProduct.ListclsColorProduct.Count; j++)
{
}
}
}
list.InnerHtml = str;
filters.InnerHtml = result;
// sugg.InnerHtml = str1;
}
}
最佳答案
您可以将复选框包装在容器中,使用该容器的 ID 来获取所有复选框。
<div id="checkBoxList">
<label>
<input type="checkbox" name="chk" id="chk1" value="casual">casual</label><br>
<label>
<input type="checkbox" name="chk" id="chk2" value="intermediate">intermediate</label><br>
</div>
然后更改 JavaScript 中的行
$('[id^="chk"]').each(function (i, elem) {
到此
$('#checkBoxList input[type="checkbox"]').each(function (i, elem) {
Ps,您的函数不再工作的原因是 objcolor.ListclsColor[i].ColorID
可能不包含 chk
关于javascript - 我想将文本与我的动态复选框匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41758218/