【问题标题】:Stay focus in a field when field still empty当字段仍然为空时,保持焦点在字段中
【发布时间】:2016-01-19 17:00:18
【问题描述】:

我有两个输入字段,分别是“电话”字段和“姓名”字段。我希望光标在“电话”字段仍然为空时保持焦点。因为当我在字段为空时单击“选项卡”时,会出现警报,并且在我单击“确定”后光标移动到“名称”字段。如果仍然是空的,如何使光标保持在“电话”上?

这是我的代码:

    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>

【问题讨论】:

    标签: javascript jquery jsp


    【解决方案1】:

    我猜你需要稍微改变一下:

    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.
       }
    }
    

    【讨论】:

    • 你有 onchange/onblur 一起,而不是你可以不显眼地使用。
    【解决方案2】:

    是的,我找到了自己问题的答案。我改了:

    function whenEmpty(field) {
           if (field.value == '') {
              alert("Please fill the field.");
              field.focus();
           }  
        }
    

    【讨论】:

    • 但在此之前我没有看到哈哈哈请原谅我:v
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多