Javascript 通过函数参数传递id
试图通过将ID传递给函数参数来获取文本框的值。这应该很容易,但我不能绕着它转 JavaScript: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!');
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;
}