将html值传递到javascript函数中 [英] Passing html values into javascript functions

查看:110
本文介绍了将html值传递到javascript函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个javascript函数,需要在其中确认输入.我编写了以下代码,但即使输入了有效值,它也会给出负值,即"else"部分.有人可以提出解决方案吗?

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?

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文件:

Javascript File:

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

    }
}

推荐答案

这是 JSfiddle演示

我更改了您的HTML,并为您输入的文本字段提供了一个值ID.我为您的verifyorder函数删除了传递的参数,而是使用document.getElementById();来获取文本字段的内容;然后我用+order将str转换为值,以便您可以检查它是否大于零:

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:

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

这篇关于将html值传递到javascript函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆