简体   繁体   English

将html值传递给javascript函数

[英]Passing html values into javascript functions

I was making a javascript function in which I need to confirm the input. 我正在制作一个javascript函数,我需要确认输入。 I wrote the following code but its giving negative value ie "else" part even if i enter a valid value. 我写了下面的代码,但即使输入有效值,它给出负值即“其他”部分。 Can some one please suggest a solution? 有人可以建议一个解决方案吗?

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"; 

    }
}

给文本框一个id为“txtValue”并将输入按钮声明更改为以下内容:

<input type="button" value="submit" onclick="verifyorder(document.getElementById('txtValue').value)" />

Here is the JSfiddle Demo 这是JSfiddle演示

I changed your HTML and give your input textfield an id of value. 我更改了您的HTML,并为您的输入文本字段提供了有价值的ID。 I removed the passed param for your verifyorder function, and instead grab the content of your textfield by using document.getElementById(); 我删除了验证顺序函数的传递参数,而是使用document.getElementById()获取文本字段的内容。 then i convert the str into value with +order so you can check if it's greater than zero: 然后我用+order将str转换为值,这样你就可以检查它是否大于零:

<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";
        }
    }

只需在输入文本字段中输入id属性 -

<input type="text" maxlength="3" name="value" id="value" />

尝试: if(parseInt(order)>0){....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM