【发布时间】:2015-01-01 01:16:51
【问题描述】:
我创建了一个表格格式的 div,用于过滤数据选项。 我在从文本框中按 Enter 时遇到问题。当我按下回车键时,会出现警报,但页面只是重新加载并且没有使用任何选项正确过滤。谁能给我解释一下这是怎么回事?
如果我不清楚或需要解释什么,请告诉我!
这是我的过滤选项栏的样子:
HTML:
<form name="myform">
<center><table id="mykeyTable" border="1" cellspacing="10" cellpadding="10">
<center></center>
</td>
<!--Search bar -->
<tr>
<td colspan="4">
<center>
<form>
Starts With: <input type="text" id="myText" value=""><br>
</form>
</center>
</tr>
<!-- Site Options -->
<td><center>Site</center><br>
<input type="radio" name="siteID" onclick="updateSiteID(this.value)" value="Asc"checked>
<input type ='button' id='siteIDChosen' class='button-addAsc'/>
<br>
<input type="radio" name="siteID" onclick="updateSiteID(this.value)" value="Desc" >
<input type ='button' id='siteIDChosen' class='button-addDesc'/>
<br>
</input>
</form></td>
<!-- Status options -->
<td><center>Status
<br><br></center><input type="radio" name="siteStatus" onclick="updateSiteStatus(this.value)"value="Online"> Online<br><input type="radio" name="siteStatus" onclick="up\
dateSiteStatus(this.value)" value="Offline"> Offline<br><input type="radio" name="siteStatus" onclick="updateSiteStatus(this.value)" value="Both" checked> Both </form></td\
>
<!-- usage plan -->
<td><center>Usage Plan (in MB)</center>
<br><input type="radio" name="usagePlan" onclick="updateUsagePlan(this.value)" value="yesUsagePlan"> Data Plan<br><input type="radio" name="usagePlan" onclick="updateUsage\
Plan(this.value)" value="noUsagePlan"> No Data Plan<br><input type="radio" name ="usagePlan" onclick="updateUsagePlan(this.value)" value="bothUsagePlan" checked> All Plans\
</form></td>
</table>
<br>
<form><input type='button' id='filter' name='Submit' onclick='validate()' class='button-add' value=" Filter Selections"/></form>
</center>
</form>
</div>
Javascript:
<script>
$("#myText").keypress(function(event) {
if (event.which == 13) {
alert("You pressed enter");
validate();
}
});
$(document).keypress(function(event){
if(event.keyCode == 13){
validate();
}
});
function validate() {
//...
}
</script>
已解决:
我使用 jQuery click 来调用与按钮相同的函数。
$(document).keypress(function(event){
if(event.keyCode == 13){
$('#filter').click();
// validate(); doesn't need to be called from here
}
});
【问题讨论】:
-
您的解决方案仅在我输入文档时有效,但如果我专注于 inputText 并按 Enter,它将覆盖 '$('#filter').click()' 方法并调用所有其他可用方法在脚本中。知道是什么问题吗??
标签: javascript jquery html forms keypress