将两个隐藏输入与简单javascript进行比较失败

将两个隐藏输入与简单javascript进行比较失败,javascript,php,html,algorithm,hidden-field,Javascript,Php,Html,Algorithm,Hidden Field,我得到了两个隐藏的输入HTML,我想与javascript onclick submit按钮进行比较。但即使它看起来简单明了,也不会起作用 功能是: function check() { if ((parseFloat(document.getElementById("price_sell").value) < (parseFloat(document.getElementById("price").value)*0.95)) OR (parseFloat(document.getEl

我得到了两个隐藏的输入HTML,我想与javascript onclick submit按钮进行比较。但即使它看起来简单明了,也不会起作用

功能是:

function check() {
  if ((parseFloat(document.getElementById("price_sell").value) < (parseFloat(document.getElementById("price").value)*0.95)) OR (parseFloat(document.getElementById("price_sell").value) > (parseFloat(document.getElementById("price").value)*1.05)) ){
     alert("too high/low!");
  }
}
函数检查(){
如果((parseFloat(document.getElementById(“price_sell”).value)<(parseFloat(document.getElementById(“price”).value)*0.95)或(parseFloat(document.getElementById(“price_sell”).value)>(parseFloat(document.getElementById(“price”).value)*1.05))){
警报(“过高/过低!”);
}
}
输入文本如下:

    <input type="hidden" id="price" name="price" value="<?php echo $prc ?>" />
    <input type="hidden" id="price_sell" name="price_sell" />

将运算符或更改为| |。
如果此代码是JS代码,则由于语法错误而无法工作。

使用数字(变量)将文本转换为数字,然后进行比较

例如:-

var price = Number(document.getElementById(price).value);
var price_sell = Number(document.getElementById(price_sell).value);
var compare = price_sell - price;
也可以使用typeof检查变量类型

如果值为
null
undefined
,则数字函数将其转换为0(零)

即使“价格”是“价格”的两倍大/小-

最新答复2:
/*
错误-价格='10$';表示删除货币符号
或price_sell和price variable中的字符
使用trim()删除空白。
我重新检查了我的代码,发现括号不见了,于是又发了一次。
*/
price_sell=Number(price_sell.trim());
价格=数字(price.trim());
如果((价格卖出>(价格/0.80))&(价格卖出<(价格*1.30))){
//好
}否则{
//坏的
}

首先将@Rakesh_Kumar提到的值设置为
price\u sell
输入字段,然后在下面的代码中尝试

函数检查(){
var price\u sell=parseFloat(document.getElementById(“price\u sell”).value);
var price=parseFloat(document.getElementById(“price”).value);
如果((价格卖出<(价格*0.95))(价格卖出>(价格*1.05))){
警报(“过高/过低!”);
} 
}

price\u sell
price
值存储在JS变量中,以便更好地读取。仅供参考,由于缺少括号和
的使用,出现了一些语法错误,我已将其替换为
|

您必须通过| |更改OR,并为价格|增加一个值

使用此示例进行测试:

<!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>
<link href="estilos/estilos.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Test</title>
<script type="text/javascript">
  function check() {
  var price_sell = parseFloat(document.getElementById("price_sell").value);
  var price = parseFloat(document.getElementById("price").value);

  if ((price_sell < (price * 0.95)) || (price_sell > (price * 1.05))) {
     alert("too high/low!");
  }
}
</script>

</head>
<body>
    <input type="hidden" id="price" name="price" value="2" />
    <input type="hidden" id="price_sell" name="price_sell" value="4"/>

<input type="button"
onclick="check()"
value="Check">
</body>
</html>

测验
函数检查(){
var price\u sell=parseFloat(document.getElementById(“price\u sell”).value);
var price=parseFloat(document.getElementById(“price”).value);
如果((价格卖出<(价格*0.95))(价格卖出>(价格*1.05))){
警报(“过高/过低!”);
}
}

其中是为具有id的输入标记设置的值
price\u sell
?…如果未指定任何内容,则
(parseFloat(document.getElementById(“price\u sell”).value)
将返回
NaN
首先在price\u sell中添加值。然后重试。出于测试目的,将它们设置为文本字段。而且,javascript中没有类似于
文本的内容…您应该使用
|
,称为逻辑或运算符…值在那里,我已通过取消隐藏文本字段进行检查,它由另一个字符填充er函数。好的,我将尝试II。嗨,它仍然不起作用。我感到困惑。我将发布一张示例图片。用| |更改OR,但仍然不起作用。它不会触发警报。这并不能解决问题。主要问题是OR,必须由| | parsefloat执行。顺便说一句,即使我将parsefloat更改为number,它仍然不起作用。
<!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>
<link href="estilos/estilos.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Test</title>
<script type="text/javascript">
  function check() {
  var price_sell = parseFloat(document.getElementById("price_sell").value);
  var price = parseFloat(document.getElementById("price").value);

  if ((price_sell < (price * 0.95)) || (price_sell > (price * 1.05))) {
     alert("too high/low!");
  }
}
</script>

</head>
<body>
    <input type="hidden" id="price" name="price" value="2" />
    <input type="hidden" id="price_sell" name="price_sell" value="4"/>

<input type="button"
onclick="check()"
value="Check">
</body>
</html>