我有一个包含许多文本字段的表单,这些字段是动态生成的,其中一些字段被禁用。我希望当用户在任何文本框中按Enter键时,我将焦点移动到下一个启用的文本框。目前我正在使用以下脚本停止在按键上提交表单。
的Javascript
<script type="text/javascript" >
$(document).keypress(function (e) {
if (e.which == 13 && e.target.tagName != 'TEXTAREA') {
//Need to set focus to next active text field here.
return false;
}
});
</script>
HTML
<table class='workshoptable grdstyle' cellPadding='5px;' style='border:1px solid #cccccc' id='wrokshoptable_12'>
<tr class='grdHeader'>
<th style='width:140px;'>
Duration
</th>
<th title='Location for this workshop'>
Location
</th>
<th>
Students
</th>
<th>
Grade
</th>
<th>
Teacher
</th>
</tr>
<tr class='trAlternate'>
<td style='vertical-align:middle' title='Workshop duration'>
<div>
12: 00 - 13: 30
</div>
<input type='hidden' class='workshopDuration' value='12:00 - 13:30' />
< input type='hidden' class='templateName' value='Body Builders' />
< input type='hidden' class='templateMaxCount' value='60.0000000000' />
</td>
<td>
<input type='text' title='Enter the location for this workshop' class='textbox SchoolShowlocation' size='200' style='width:135px;' maxLength='200' disabled='disabled'>
<div class='locationValidationDiv text-danger'>
</div>
</td>
<td>
<input type='text' style='width:70px;' title='Enter number of studens attending who will attend this workshop' class='onlyNumberInput textbox NumStudent' name='2565243f-67ab-e411-9434-00155d00cd05' size='4' maxLength='4'
disabled='disabled'>
<div class='NoOfStudentsValidationDiv text-danger'>
</div>
</td>
<td>
<input type='text' class='textbox WorkshopGrade' title='Enter Grade attending this workshop' style='width:80px;' size='200' maxLength='200' disabled='disabled'>
<div class='gradeValidationDiv text-danger'>
</div>
</td>
<td>
<input type='text' class='textbox SchoolTeacherName' title='Enter name of the teacher who will be with the students' size='200' maxLength='200' style='width:135px;' disabled='disabled'>
<div class='teacherValidationDiv text-danger'>
</div>
</td>
</tr>
</table>
我尝试了很多解决方案但不适合我。
答案 0 :(得分:3)
这将完成你的工作。
$(document).keypress(function (e) {
if (e.which == 13 && e.target.tagName != 'TEXTAREA') {
var txt = $(e.target);
var allOther= $("input[type=text]:not([disabled])");
var index = jQuery.inArray(txt[0], allOther);
var next= $(allOther[index+1]);
if(next) next.focus();
debugger;
//Need to set focus to next active text field here.
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
<br><br>
<input type="text">
<br><br>
<input type="text" disabled>
<br><br>
<input type="text">
<br><br>
<input type="text" disabled>
<br><br>
<input type="text">