我有两个输入字段"电话"字段和"名称"领域。我希望光标保持专注于" Phone"当它还是空的时候。因为当我点击" tab"当字段为空时,出现警报,单击“确定”后,光标移动到"名称"领域。如何让光标专注于"电话"如果它仍然是空的?
有我的代码:
function whenEmpty(field) {
if (field.value == '') {
alert("Please fill the field.");
}
return false;
}
当我调用该函数时:
<tr>
<td><div class="required"><label>Phone</label></div></td>
<td><form:input path="connote.shipperPhone" id="shipperPhone" name="shipperPhone"
onchange="loadCustomerData()" onfocus="this.select();" onmouseup="return false;" onblur="whenEmpty(this);"
htmlEscape="true" size="20" maxlength="50" tabindex="3"/> <form:errors
path="connote.shipperPhone" cssClass="error" /></td>
</tr>
答案 0 :(得分:0)
我想你需要稍微改变一下:
function whenEmpty(field) {
if (field.value == '') {
alert("Please fill the field.");
field.focus(); // <----add this to focus
return false; // <-----move this in case of blank field only.
}
}
答案 1 :(得分:0)
function whenEmpty(field) {
if (field.value == '') {
alert("Please fill the field.");
field.focus();
}
}