【问题标题】:Input Validation onblur focus输入验证模糊焦点
【发布时间】:2014-12-01 01:13:43
【问题描述】:

无法让它工作

  <script type="text/javascript">
  //<![CDATA[
  <!--
   function checkPrice(form) {
     var input=0;
     input=document.stickerPrice.value;
        if (input<100000) {               
            alert("Sticker price must be more than $100,000");
            document.getElementsByName("stickerPrice").focus();
    }
 }

       // -->
       //]]>
     </script>

这是标签部分。

   <form action=''>

    <p>Sticker Price on the Car (in dollars):
    <input type="text" name="stickerPrice" size="15" onblur="checkPrice(this.formData)" /></p>
    </form>

如果 onblur 事件少于 10,000,我希望出现一个警报框,然后将焦点放回该文本框。

【问题讨论】:

    标签: javascript validation input focus onblur


    【解决方案1】:

    您可以通过调用来传递输入本身

    onblur="checkPrice(this.formData)"

    然后你可以通过调用直接检查它的值

    form.value
    

    并聚焦

    form.focus();

    这是一个工作示例。

    function checkPrice(form) {
         var input=0;
         input=form.value;
            if (input<100000) {               
                alert("Sticker price must be more than $100,000");
                form.focus();
        }
     }
    <form action=''>
        <p>Sticker Price on the Car (in dollars):
        <input type="text" name="stickerPrice" size="15" onblur="checkPrice(this);" /></p>
        </form>

    【讨论】:

      【解决方案2】:

      我建议您使用 onblur 解决并删除 focus()。

      function checkPrice(form) {
           var input=0;
           input=form.value;
              if (input<100000) {               
                  alert("Sticker price must be more than $100,000");
                  form.value='';            
          }
       }
       <form action=''>
          <p>Sticker Price on the Car (in dollars):
          <input type="text" name="stickerPrice" size="15" onblur="checkPrice(this);" /></p>
          </form>

      【讨论】:

      • 您好,欢迎过去 ? 这是 2014 年提出的问题,但您的回答将帮助未来的开发人员✌
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 2019-01-04
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多