Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.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 通过函数参数传递id_Javascript_Html_Function_Getelementbyid - Fatal编程技术网

Javascript 通过函数参数传递id

Javascript 通过函数参数传递id,javascript,html,function,getelementbyid,Javascript,Html,Function,Getelementbyid,试图通过将ID传递给函数参数来获取文本框的值。这应该很容易,但我不能绕着它转 JavaScript: function checkfield(id) { var field = document.getElementById(id); if (isNaN(field) == true) { alert('You must enter a valid number!');

试图通过将ID传递给函数参数来获取文本框的值。这应该很容易,但我不能绕着它转

JavaScript:

function checkfield(id) 
    {
        var field = document.getElementById(id);

        if (isNaN(field) == true) 
            {
                alert('You must enter a valid number!');
                field.value = "0";
                field.focus(textbox);
                field.select(textbox);
                return false;
            }
        return true;

    }
HTML:


错误消息

错误:无法获取未定义或空引用的属性“值”


您输入的id是“19”而不是“numberbox19”

您输入的id是“19”而不是“numberbox19”

您的id是
19
,但您正在将
numberbox19
传递给Javascript函数。还有几个语法错误

尝试:


和Javascript:

function checkfield(field) // <-- we passed the field in directly from the HTML, using 'this'
{
    alert(field.value);

    if (isNaN(field.value)) // check the value, not the field itself!
    {
        alert('You must enter a valid number!');
        field.value = "0";
        field.focus(); // not "field.focus(textbox);"
        return false;
    }
    return true;

}

函数checkfield(field)//您的ID是
19
,但您正在将
numberbox19
传递给Javascript函数。还有几个语法错误

尝试:


和Javascript:

function checkfield(field) // <-- we passed the field in directly from the HTML, using 'this'
{
    alert(field.value);

    if (isNaN(field.value)) // check the value, not the field itself!
    {
        alert('You must enter a valid number!');
        field.value = "0";
        field.focus(); // not "field.focus(textbox);"
        return false;
    }
    return true;

}

函数checkfield(field)//你真的是指
'numberbox19'
?因为您的字段名为“customerZip”,ID为“19”…而邮政编码字段触发另一个类似的数字字段的验证有点奇怪…您为
getValue
定义了什么(您称之为它,但它没有显示)@cHao很抱歉,代码已更新。@scunlife正在测试某些内容,但忘记删除,抱歉,更新了代码。你真的是说
'numberbox19'
?因为您的字段名为“customerZip”,ID为“19”…而邮政编码字段触发另一个类似的数字字段的验证有点奇怪…您为
getValue
定义了什么(您称之为它,但它没有显示)@cHao很抱歉,代码已更新。@scunlife正在测试某些内容,但忘记删除,抱歉,我更新了密码。正是我需要的。忘了用这个了,一旦你发了,我马上就拿到了。这正是我需要的。忘了用这个了,一旦你发了,我马上就拿到了。
function checkfield(field) // <-- we passed the field in directly from the HTML, using 'this'
{
    alert(field.value);

    if (isNaN(field.value)) // check the value, not the field itself!
    {
        alert('You must enter a valid number!');
        field.value = "0";
        field.focus(); // not "field.focus(textbox);"
        return false;
    }
    return true;

}