如何使用 JavaScript 检查 HTML table 中插入的值的类型
How to check type of value inserted in HTML table using JavaScript
我正在制作 contenteditable HTML table。我必须确保只能插入数值,并在用户尝试插入字符串或非数字的内容时显示警报。我怎样才能做到这一点?我在网上搜索过,但我还没有找到任何东西。
btw,我用提示来沟通:有没有办法拒绝用户勾选"don't let the browser keep prompting things"选项?
使用 typeof 运算符:http://www.w3schools.com/jsref/jsref_operators.asp。将其包装在 if 语句中并阻止用户继续。
使用 typeof 运算符 -- 例如;
var val = your value.
if('number' == typeof val)
{
//your logic
}
你应该用id来处理你的数据表,这样你就可以用js函数typeof检查输入类型值。这是一个例子:enter link description here
像这样:
var inputVal= document.getElementById("yourTableId");
if('string' == typeof inputVal)
{
alert("This is a string!");
}
else if('NaN' == typeof inputVal)
{
alert("This is not number!");
}
希望对您有所帮助;)
我正在制作 contenteditable HTML table。我必须确保只能插入数值,并在用户尝试插入字符串或非数字的内容时显示警报。我怎样才能做到这一点?我在网上搜索过,但我还没有找到任何东西。
btw,我用提示来沟通:有没有办法拒绝用户勾选"don't let the browser keep prompting things"选项?
使用 typeof 运算符:http://www.w3schools.com/jsref/jsref_operators.asp。将其包装在 if 语句中并阻止用户继续。
使用 typeof 运算符 -- 例如;
var val = your value.
if('number' == typeof val)
{
//your logic
}
你应该用id来处理你的数据表,这样你就可以用js函数typeof检查输入类型值。这是一个例子:enter link description here
像这样:
var inputVal= document.getElementById("yourTableId");
if('string' == typeof inputVal)
{
alert("This is a string!");
}
else if('NaN' == typeof inputVal)
{
alert("This is not number!");
}
希望对您有所帮助;)