Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.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_Jquery_Html_Css - Fatal编程技术网

Javascript 似乎无法获得';。焦点()';要工作的元素。。。

Javascript 似乎无法获得';。焦点()';要工作的元素。。。,javascript,jquery,html,css,Javascript,Jquery,Html,Css,正如标题所示,我似乎无法获得“.focus();”工作。 我不知道为什么,据我所知,它应该工作得很好 我希望这样,当按下警报的“确定”按钮时,光标将准备好输入“用户名”文本框 先谢谢你。代码如下 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=

正如标题所示,我似乎无法获得“.focus();”工作。 我不知道为什么,据我所知,它应该工作得很好

我希望这样,当按下警报的“确定”按钮时,光标将准备好输入“用户名”文本框

先谢谢你。代码如下

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Com 311 | Week 9</title>

    <script type="text/javascript">

        function validName(){

            var Name = document.userSurvey.userName.value;

            if (Name=="")
            {
                alert ("Please enter your name.");
                document.getElementById('userName').focus();
            }

        }

    </script>

    </head>

    <body>
        <h1> User Survey </h1>
        <hr />
        <h2> User Information </h2>
        <h3>Please enter your details below:</h3>

    <form name="userSurvey" onsubmit="validName()">
        <p> Full Name: (required) </p>
        <input type="text" size="30" name="userName"/>


        <p> Email Address: (required) </p>
        <input type="text" size="50" name="email"/>


        <p> Phone Number: (required)</p>
        <input type="number" size="30" name="phone"/>

        <hr />

        <p> Please choose your favourite type of book:</p>

        <input type="checkbox" name="bookChoice0" value="Action">Action 
        <input type="checkbox" name="bookChoice1" value="Comic">Comic
        <input type="checkbox" name="bookChoice2" value="Adventure">Adventure 
        <input type="checkbox" name="bookChoice3" value="Fantasy">Fantasy
        <br /><br />

        <input type="textarea" name="textarea"/>
        <br /><br />

        <input type="submit" value="Submit"/>
        <input type="reset" value="reset"/>
    </form>

    </body>
    </html>

Com 311第9周
函数validName(){
var Name=document.userSurvey.userName.value;
如果(名称==“”)
{
警告(“请输入您的姓名”);
document.getElementById('userName').focus();
}
}
用户调查

用户信息 请在下面输入您的详细信息: 全名:(必选)

电子邮件地址:(必填)

电话号码:(必填)


请选择您最喜欢的书籍类型:

行动 漫画 冒险 幻想




存在多个问题

  • 如果验证失败,validate方法必须返回false
  • 没有ID为userName的元素
  • 试一试


    演示:

    当你遇到这样的问题时,你应该让别人更容易帮助你

    e、 你可以把它作为一个JSFiddle,这一次我已经为你做了

    我也已经加入了修复程序

    上面显示的修复程序,以及以下修复程序:

        <input type="text" size="30" name="userName" id="userName" />
    
    
    
    您没有阻止提交,非常感谢!工作得很好!
    function validName() {
        var Name = document.userSurvey.userName.value;
        if (Name == "") {
            alert("Please enter your name.");
            document.userSurvey.userName.focus();
            return false;
        }
    
    }
    
        <input type="text" size="30" name="userName" id="userName" />