Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.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/1/ms-access/4.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函数的html文件出错_Javascript_Html_Textbox - Fatal编程技术网

使用javascript函数的html文件出错

使用javascript函数的html文件出错,javascript,html,textbox,Javascript,Html,Textbox,我开始体验神奇的Javascript世界,我正在制作一个带有Javascript函数和一个框的html文件,事实是警报没有显示进入文本字段的内容,我做错了什么?这是我的代码 <html> <head> <script> function showMe() { var nombre= cnombre.value; alert("You are "+ nombr

我开始体验神奇的Javascript世界,我正在制作一个带有Javascript函数和一个框的html文件,事实是警报没有显示进入文本字段的内容,我做错了什么?这是我的代码

<html>
    <head>
        <script>
        function showMe()
        {
            var nombre= cnombre.value;
            alert("You are "+ nombre);
            cnombre.value="";
            cnombre.focus();
        }
        </script>
    </head> 

    <body>  
            Name:<input type ="text" name="cnombre" 
    size="30">
            <input type="button" value="Go"
    onClick=showMe();>

    </body>
</html>

函数showMe()
{
var nombre=cnombre.value;
警惕(“你是”+nombre);
cnombre.value=“”;
cnombre.focus();
}
姓名:

您不能像那样访问文本框。有关基于代码的工作示例,请参见


请注意,添加到文本框的
id=“cnombre”
以及使用
document.getElementById()
更改了您的代码,为文本框指定了一个id,然后根据该id从DOM检索文本框。这应该适用于您:

<html>
<head>
    <script>
    function showMe() {
    var nombre = document.getElementById('cnombre');
    alert("You are " + nombre.value);
    nombre.value = "";
    nombre.focus();
}
    </script>
</head> 

<body>  
        Name:<input type ="text" id="cnombre" name="cnombre" 
size="30">
        <input type="button" value="Go"
onClick=showMe();>

</body>

函数showMe(){
var nombre=document.getElementById('cnombre');
警报(“您是”+标称值);
nombre.value=“”;
nombre.focus();
}
姓名: