Javascript textbox-js中只有数值和小于6的值

Javascript textbox-js中只有数值和小于6的值,javascript,php,jquery,validation,numeric,Javascript,Php,Jquery,Validation,Numeric,只有数值和小于6的值(可以是5.9,4.6…但不是6.01)的构建逻辑应该从输入中排除,已经尝试了一些东西并得到了解决方案,但面临一些问题 下面的输入在PHP循环中是动态的: <input type="text" name="prd_<?php echo $prd['id']; ?>" id="prd_<?php echo $prd['id']; ?>" onkeypress="return chkvalue(<?php echo $prd['id']; ?&

只有数值和小于6的值(可以是5.9,4.6…但不是6.01)的构建逻辑应该从输入中排除,已经尝试了一些东西并得到了解决方案,但面临一些问题

下面的输入在PHP循环中是动态的:

<input type="text" name="prd_<?php echo $prd['id']; ?>" id="prd_<?php echo $prd['id']; ?>" onkeypress="return chkvalue(<?php echo $prd['id']; ?>,<?php echo $prd['wgh']; ?>,<?php echo $prd['price']; ?> ); "/>

<input type="text" name="prdinto_<?php echo $prd['id']; ?>" id="prdinto_<?php echo $prd['id']; ?>" />
<input type="text" name="prd_1" id="prd_1" onkeypress="return chkvalue(1,0.50,1430.00); "/> 
<input type="text" name="prdinto_1" id="prdinto_1" />

<input type="text" name="prd_2" id="prd_2" onkeypress="return chkvalue(2,0.20,1130.00); "/> 
<input type="text" name="prdinto_2" id="prdinto_2" />

<input type="text" name="prd_3" id="prd_3" onkeypress="return chkvalue(3,0.55,1340.00); "/> 
<input type="text" name="prdinto_3" id="prdinto_3" />
function chkvalue(id,wgh,price)
{ 
    var number = document.getElementById("prd_"+id).value;
    if (isNaN(number.trim())) {
        alert("Enter only numbers.");
        document.getElementById("prd_"+id).value = "";  
    }

    if ( number.trim() > 6 ) {
        alert("Value entered must be 6 or lower.");
        document.getElementById("prd_"+id).value = "";
    }

    var ca = wgh * price;

     document.getElementById("prdinto_"+id).value = ca ;
     //document.getElementById("prdinto_"+id).value = ca;
}
我面临的问题:

<input type="text" name="prd_<?php echo $prd['id']; ?>" id="prd_<?php echo $prd['id']; ?>" onkeypress="return chkvalue(<?php echo $prd['id']; ?>,<?php echo $prd['wgh']; ?>,<?php echo $prd['price']; ?> ); "/>

<input type="text" name="prdinto_<?php echo $prd['id']; ?>" id="prdinto_<?php echo $prd['id']; ?>" />
<input type="text" name="prd_1" id="prd_1" onkeypress="return chkvalue(1,0.50,1430.00); "/> 
<input type="text" name="prdinto_1" id="prdinto_1" />

<input type="text" name="prd_2" id="prd_2" onkeypress="return chkvalue(2,0.20,1130.00); "/> 
<input type="text" name="prdinto_2" id="prdinto_2" />

<input type="text" name="prd_3" id="prd_3" onkeypress="return chkvalue(3,0.55,1340.00); "/> 
<input type="text" name="prdinto_3" id="prdinto_3" />
function chkvalue(id,wgh,price)
{ 
    var number = document.getElementById("prd_"+id).value;
    if (isNaN(number.trim())) {
        alert("Enter only numbers.");
        document.getElementById("prd_"+id).value = "";  
    }

    if ( number.trim() > 6 ) {
        alert("Value entered must be 6 or lower.");
        document.getElementById("prd_"+id).value = "";
    }

    var ca = wgh * price;

     document.getElementById("prdinto_"+id).value = ca ;
     //document.getElementById("prdinto_"+id).value = ca;
}
1} 当我在prod_1或prod_2的输入中键入任何字母/单词时,我会收到“仅输入数字”的警告,但在第一个字母之后是:如果我键入a,则不会发出警告,但只要我再次键入任何字母,我就会收到警告。。。但我不想这样..我想在第一个字母它应该识别并发出警报

2} 第二个警报“输入的值必须为6或更低”-当我在这里键入6.01时,它也接受值,并且没有警报..我希望值应低于6…但它可以接受十进制值,如4.5或4.6

3} 当我使标签,然后只有它显示警报,需要在鼠标移动警报,按键等

4} 最后,它们没有得到价值

需要-获得1,2,3分


有时我会在文本框中看到“未定义”,首先,应该使用onkeypress事件,而不是onkeypress事件。因此,请使用onkeyup()方法而不是onkeypress()方法。这样,大多数问题都将消失

    <!DOCTYPE html>
        <table>
        <tr>
          <td>
            <input type="text" placeholder="prd_1" name="prd_1" id="prd_1" onkeyup="chkvalue(1,0.50,1430.00)"/> 
          </td>
          <td>
                <input type="text"  placeholder="prdinto_1" name="prdinto_1" id="prdinto_1" />
          </td>
        </tr>
        <tr>
          <td>
            <input type="text" placeholder="prd_2" name="prd_2" id="prd_2" onkeyup="chkvalue(2,0.20,30.00); "/> 
          </td>
          <td>
             <input type="text" placeholder="prdinto_2" name="prdinto_2" id="prdinto_2" />
          </td>
        </tr>
        <tr>
          <td>
               <input type="text" placeholder="prd_3" name="prd_3" id="prd_3" onkeyup="chkvalue(3,0.55,10.00); "/> 
          </td>
          <td>
                <input type="text" placeholder="prdinto_3" name="prdinto_3" id="prdinto_3" />
          </td>
        </tr>
        </table>
      <script>  
        function chkvalue(id,wgh,price)
    { 
        var number = document.getElementById("prd_"+id).value;
        if (isNaN(parseInt(number.trim()))) {
            alert("Enter only numbers.");
            document.getElementById("prd_"+id).value = "";  
        }

        if ( number.trim() > 6 ) {
            alert("Value entered must be 6 or lower.");
            document.getElementById("prd_"+id).value = "";
        }

        var ca = wgh * price;
        document.getElementById("prdinto_"+id).value = ca;
        //document.getElementById("prdinto_"+id).value = ca;

 if (isNaN(parseInt(number.trim()))) {
            alert("Enter only numbers.");
            document.getElementById("prd_"+id).value = "";  
            document.getElementById("prdinto_"+id).value = "";
        }

    }

    </script>

函数chkvalue(id、wgh、价格)
{ 
var编号=document.getElementById(“prd_216;”+id).value;
if(isNaN(parseInt(number.trim())){
警告(“仅输入数字”);
document.getElementById(“prd_216;”+id).value=“”;
}
如果(number.trim()>6){
警报(“输入的值必须为6或更低”);
document.getElementById(“prd_216;”+id).value=“”;
}
var ca=wgh*价格;
document.getElementById(“prdinto”+id).value=ca;
//document.getElementById(“prdinto”+id).value=ca;
if(isNaN(parseInt(number.trim())){
警告(“仅输入数字”);
document.getElementById(“prd_216;”+id).value=“”;
document.getElementById(“prdinto_u3;”+id).value=“”;
}
}

这段代码运行得非常好!:)检查一下,

循环不存在,你只显示了一个输入。(1)AFIK,<代码>键按下< /代码>发生在文本输入之前,考虑使用<代码> OnKyUp ,实际上,更好的做法是不使用内联事件,而是使用<代码> AdvestTristaNe/Cuff>。(2) 使用parseFloat将其从字符串转换为数字。为什么不使用输入模式属性?@Script47,抱歉键入错误..已更正..请chk@Script47感谢将按键更改为onkeyup。。并开始在第一个字母时发出警报…但使用parseFloat(number.trim())时。。。然后没有警报…因此更改回isNaN(number.trim())。。但最后一点仍然不起作用。如果不预先输入jquery,请使用onkeyup()方法,而不是onkeypress()方法。-但这两个都是jQuery函数,您只解决了问题1,其余的呢?@Tharushi谢谢…但我不能,因为prd_1和prd_2..1,2是数据库中的动态数字..因此我不能将其静态化。您能帮我@document.getElementById(“prdinto_”+id)。value=ca;document.getElementById(“prdinto”+id).value=ca。。。但是在第一个prdinto_1上获得值,但是prdinto_2和prdinto_3显示0值检查我答案中的代码。我更新了它,它的工作非常好!