데이터 옵션을 필터링하는 데 사용하는 테이블 형식으로 div를 만들었습니다. 텍스트 상자 안쪽에서 Enter 키를 누르는 데 문제가 있습니다. 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>
자바 스크립트 :
<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를 버튼으로 사용 된 것과 같은 함수를 호출합니다 사용된다.
$(document).keypress(function(event){
if(event.keyCode == 13){
$('#filter').click();
// validate(); doesn't need to be called from here
}
});
귀하의 솔루션은 내가 문서에 입력 한 경우에만 작동하지만 내가 inputText에 집중하고 Enter 키를 누르면 '$ ('# filter '). click()'메서드를 무시하고 all 다른 방법은 스크립트에서 사용할 수 있습니다. 어떤 생각이 문제인지 ?? –