我的站点中有以下 JavaScript:
$(function() {
var $cells = $("td");
$("#search").keyup(function() {
var val = $.trim(this.value).toUpperCase();
if (val === "")
$cells.parent().show();
else {
$cells.parent().hide();
$cells.filter(function() {
return -1 != $(this).text().toUpperCase().indexOf(val);
}).parent().show();
}
});
});
此示例适用于所有浏览器,因此我认为问题出在我的 HTML 某处。这是相关部分:
<div id="searchContainer">
<input id="search" type="text">
</div>
<table>
<tr>
<th>Username</th>
<th>Full name</th>
<th>Tick to select</th>
</tr>
@foreach (var user in result) {
<tr><td>@user.Username</td> <td>@user.FirstName @user.SecondName</td>
<td><input type="checkbox" name="userId" value="@user.UserId" /></td></tr>
}
我认为错误一定出在这里,因为代码在 Firefox 中运行良好,但在 Chrome 或 IE 中根本无法运行。
最佳答案
可能是因为您在输入搜索框上缺少结束符/...
<input id="search" type="text">
应该是
<input id="search" type="text" />
如果没有,请尝试发布生成的源代码,以确保客户端实际看到的是什么而不是 asp.net 看到的(我假设您正在使用带有 razor 的 Asp.net mvc?)。
关于JavaScript 可以在 Firefox 中运行,但不能在 IE 或 Chrome 中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9753209/