将 html 值传递给 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5315946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing html values into javascript functions
提问by Frustrated Coder
I was making a javascript function in which I need to confirm the input. I wrote the following code but its giving negative value i.e. "else" part even if i enter a valid value. Can some one please suggest a solution?
我正在制作一个 javascript 函数,我需要在其中确认输入。我编写了以下代码,但即使我输入了有效值,它也会给出负值,即“其他”部分。有人可以提出解决方案吗?
Html file:
html文件:
<!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>Javascript App</title>
<script type="text/javascript" src="app1.js">
</script>
</head>
<body><h1 id="heading1" style="text-align:center; height:auto; width:auto; font-family:'Arial Black', Gadget, sans-serif">Determinant of a nxn matrix</h1>
<p id="paragraph1" style="font-family:'Arial Black', Gadget, sans-serif"> This program allows you to compute the determinant of a nxn matrix</p>
<p>
Input the order of the matrix
<br />
<input type="text" maxlength="3" name="value" />
<input type="button" value="submit" onclick="verifyorder(value)" />
</p>
<p id="error"></p>
<p id="detspace"></p>
</body>
</html>
Javascript File:
Javascript文件:
function verifyorder(order){
;
if(order>0){
return true;
}
else{
alert("Sorry, you need to enter a positive integer value, try again");
document.getElementById('error').innerHTML="Sorry, you need to enter a positive integer value, try again";
}
}
采纳答案by u476945
Here is the JSfiddle Demo
I changed your HTML and give your input textfield an id of value. I removed the passed param for your verifyorder function, and instead grab the content of your textfield by using document.getElementById(); then i convert the str into value with +order
so you can check if it's greater than zero:
我更改了您的 HTML 并为您的输入文本字段提供了一个 id 值。我删除了为您的 verifyorder 函数传递的参数,而是使用 document.getElementById(); 获取文本字段的内容;然后我将 str 转换为值,+order
以便您可以检查它是否大于零:
<input type="text" maxlength="3" name="value" id='value' />
<input type="button" value="submit" onclick="verifyorder()" />
</p>
<p id="error"></p>
<p id="detspace"></p>
function verifyorder() {
var order = document.getElementById('value').value;
if (+order > 0) {
alert(+order);
return true;
}
else {
alert("Sorry, you need to enter a positive integer value, try again");
document.getElementById('error').innerHTML = "Sorry, you need to enter a positive integer value, try again";
}
}
回答by Jon Egerton
Give the textbox an id of "txtValue" and change the input button declaration to the following:
为文本框指定“txtValue”的 id,并将输入按钮声明更改为以下内容:
<input type="button" value="submit" onclick="verifyorder(document.getElementById('txtValue').value)" />
回答by Freelancer
Simply put id attribute in your input text field -
只需将 id 属性放在您的输入文本字段中 -
<input type="text" maxlength="3" name="value" id="value" />
回答by Diodeus - James MacFarlane
Try: if(parseInt(order)>0){....
尝试: if(parseInt(order)>0){....