Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将光标集中在表单元素上?_Javascript - Fatal编程技术网

Javascript 如何将光标集中在表单元素上?

Javascript 如何将光标集中在表单元素上?,javascript,Javascript,我想在表单元素的空输入之后添加游标。第一个空表单的位置。 有了这个目标,我尝试添加this.focus()进入validate()函数。但这并没有成功 第二点-如何将brovser页面后的光标设置为第一个表单元素。我尝试了这个目标onLoad()方法进入正文。但这并没有成功 代码: <html> <head> <title>Form with check</title> <script>

我想在表单元素的空输入之后添加游标。第一个空表单的位置。
有了这个目标,我尝试添加
this.focus()进入
validate()
函数。但这并没有成功

第二点-如何将brovser页面后的光标设置为第一个表单元素。我尝试了这个目标
onLoad()方法进入
正文
。但这并没有成功

代码:

<html>
    <head>
        <title>Form with check</title>
            <script>
                function validate() {
                    if(document.form1.yourname.value.length < 1) {
                        alert("Enter your name, please");
                        this.focus();
                        return false;
                    }
                    if(document.form1.adress.value.length < 3) {
                        alert("Enter your adress, please");
                        this.focus();
                        return false;
                    }
                    if(document.form1.phone.value.length < 3) {
                        alert("Enter your phone number, please");
                        this.focus();
                        return false;
                    }
                    return true;
                }
            </script>
    </head> 
        <body>
            <h1>Form with check</h1>
            <p>Input all data. When button Submit pushed data will be sent as message.</p>  
                <form name="form1" action="mailto:user@host.com" enctype="text/plain"
                onSubmit="validate();">
                    <p><b>Name:</b><input type="text" length="20" name="yourname">
                    </p>
                    <p><b>Adress:</b><input type="text" length="20" name="adress">
                    </p>
                    <p><b>Phone:</b><input type="text" length="20" name="phone">
                    </p>
                    <input type="SUBMIT" value="Submit">                    
            </form>
            onLoad();
        </body>    
</html>

带支票的表格
函数验证(){
if(document.form1.yourname.value.length<1){
提醒(“请输入您的姓名”);
这是focus();
返回false;
}
if(document.form1.address.value.length<3){
提醒(“请输入您的地址”);
这是focus();
返回false;
}
如果(document.form1.phone.value.length<3){
提醒(“请输入您的电话号码”);
这是focus();
返回false;
}
返回true;
}
带支票的表格
输入所有数据。当按钮“提交”时,按下的数据将作为消息发送。

姓名:

地址:

电话:

onLoad();
问题:

  • 如何将此功能添加到表单

    • 你不是忘了做吗

      onSubmit="return validate();" ?
      
      document.form1.yourname.focus()替换
      this.focus()

      以下是重新编写的代码:

      <html>
          <head>
              <title>Form with check</title>
                  <script>
                      function validate() {
                          if(document.form1.yourname.value.length < 1) {
                              alert("Enter your name, please");
                              document.form1.yourname.focus();
                              return false;
                          }
                          if(document.form1.adress.value.length < 3) {
                              alert("Enter your adress, please");
                             document.form1.adress.focus();
                              return false;
                          }
                          if(document.form1.phone.value.length < 3) {
                              alert("Enter your phone number, please");
                              document.form1.phone.focus();
                              return false;
                          }
                          document.getElementById("ff").submit();
                          return true;
      
                      }
                  </script>
          </head> 
              <body >
                  <h1>Form with check</h1>
                  <p>Input all data. When button Submit pushed data will be sent as message.</p>  
                      <form id="ff" name="form1" action="mailto:user@host.com" enctype="text/plain"
                      >
                          <p><b>Name:</b><input type="text" length="20" name="yourname">
                          </p>
                          <p><b>Adress:</b><input type="text" length="20" name="adress">
                          </p>
                          <p><b>Phone:</b><input type="text" length="20" name="phone">
                          </p>
                          <input type="button" value="Submit" onclick="validate();">                    
                  </form>
      
              </body>    
      </html>
      
      
      带支票的表格
      函数验证(){
      if(document.form1.yourname.value.length<1){
      提醒(“请输入您的姓名”);
      document.form1.yourname.focus();
      返回false;
      }
      if(document.form1.address.value.length<3){
      提醒(“请输入您的地址”);
      document.form1.address.focus();
      返回false;
      }
      如果(document.form1.phone.value.length<3){
      提醒(“请输入您的电话号码”);
      document.form1.phone.focus();
      返回false;
      }
      document.getElementById(“ff”).submit();
      返回true;
      }
      带支票的表格
      输入所有数据。当按钮“提交”时,按下的数据将作为消息发送。

      姓名:

      地址:

      电话:

      还有工作

      在验证函数
      的上下文中,该
      将是全局窗口对象

      要在函数中处理表单,可以根据您的代码,使用
      document.form1
      或通过id(或其他选择器)手动调用表单,也可以将表单发送到函数:

        <script>
              function validate(sender) {
                  if(sender.yourname.value.length < 1) {
                      alert("Enter your name, please");
                      sender.focus();
                      return false;
                  }
                  if(sender.adress.value.length < 3) {
                      alert("Enter your adress, please");
                      sender.focus();
                      return false;
                  }
                  if(sender.phone.value.length < 3) {
                      alert("Enter your phone number, please");
                      sender.focus();
                      return false;
                  }
                  return true;
              }
          </script>
          <form name="form1" action="mailto:user@host.com" enctype="text/plain" onSubmit="validate(this);">
              <p>
                 <b>Name:</b><input type="text" length="20" name="yourname">
              </p>
              <p>
                 <b>Adress:</b><input type="text" length="20" name="adress">
              </p>
              <p>
                 <b>Phone:</b><input type="text" length="20" name="phone">
              </p>
              <input type="SUBMIT" value="Submit">                    
         </form>
      
      
      函数验证(发送方){
      if(sender.yourname.value.length<1){
      提醒(“请输入您的姓名”);
      focus();
      返回false;
      }
      if(sender.address.value.length<3){
      提醒(“请输入您的地址”);
      focus();
      返回false;
      }
      if(sender.phone.value.length<3){
      提醒(“请输入您的电话号码”);
      focus();
      返回false;
      }
      返回true;
      }
      
      姓名:
      

      地址:

      电话:


      为什么需要添加此部分-
      document.getElementById(“ff”).submit()这是为了提交表单。还请注意,我使用了insteadWhy添加此部分-
      “return…
        <script>
              function validate(sender) {
                  if(sender.yourname.value.length < 1) {
                      alert("Enter your name, please");
                      sender.focus();
                      return false;
                  }
                  if(sender.adress.value.length < 3) {
                      alert("Enter your adress, please");
                      sender.focus();
                      return false;
                  }
                  if(sender.phone.value.length < 3) {
                      alert("Enter your phone number, please");
                      sender.focus();
                      return false;
                  }
                  return true;
              }
          </script>
          <form name="form1" action="mailto:user@host.com" enctype="text/plain" onSubmit="validate(this);">
              <p>
                 <b>Name:</b><input type="text" length="20" name="yourname">
              </p>
              <p>
                 <b>Adress:</b><input type="text" length="20" name="adress">
              </p>
              <p>
                 <b>Phone:</b><input type="text" length="20" name="phone">
              </p>
              <input type="SUBMIT" value="Submit">                    
         </form>