简体   繁体   English

检查文本框的数量是否大于输入的文本框数组的数量

[英]Check if the number of a textbox is greater than number entered array of textbox's

I am trying to compare value of a single text box value "totalmarkstoall" with multiple array of text box's "marksscored" , the below my java script is comparing as well on key up function. 我正在尝试将单个文本框值“ totalmarkstoall”的值与文本框的多个数组“ marksscored”进行比较,以下我的Java脚本也在按键功能上进行了比较。

what am unable to do is if the value of "marksscored" that perticuler text box is greater then the "totalmarkstoall" then is shows the pop up :But it should erase the value also or should not allow to enter. 无法执行的操作是,如果该文本框的“ marksscored”值大于“ totalmarkstoall”,则显示弹出窗口:但是也应该擦除该值或不允许输入。

 function scorecompare(idval) { var marksscored = idval; var totalmarkstoall = document.getElementById("totalmarkstoall").value; if (parseInt(marksscored) > parseInt(totalmarkstoall)) { alert("greater than Total Mrks"); } else { } } 
 <input id="totalmarkstoall" type="number" style="border: 1px solid #dbdbdb;" placeholder="Enter Total Marks"></input> <table> <tr> <td> <input type="text" name="marksscored[]" id="marksscored[0]" value="" onkeyup="scorecompare(this.value);"/> </td> </tr> <tr> <td> <input type="text" name="marksscored[]" id="marksscored[1]" value="" onkeyup="scorecompare(this.value);"/> </td> </tr> <tr> <td> <input type="text" name="marksscored[]" id="marksscored[2]" value="" onkeyup="scorecompare(this.value);"/> </td> </tr> </table> 

If you do something like this you know which inputfield the value came from. 如果您执行这样的操作,您就会知道该值来自哪个输入字段。

HTML: HTML:

<table>
<tr>
    <td>
        marksscored Array of text box's
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="marksscored[]" id="marksscored[0]" value="" onkeyup="scorecompare(this);">
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="marksscored[]" id="marksscored[1]" value="" onkeyup="scorecompare(this);">
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="marksscored[]" id="marksscored[2]" value="" onkeyup="scorecompare(this);">
    </td>
</tr>

</table>

JS: JS:

function scorecompare(idval) {
   var marksscored = idval;

   var totalmarkstoall = document.getElementById("totalmarkstoall").value;
   if (parseInt(marksscored.value) > parseInt(totalmarkstoall))

   {
    alert("greater then  Total Mrks");
   //do things with inputfield marksscored
   } else {

   }
 }

You can do something like this using jquery : 您可以使用jquery做类似的事情:

  1. On keyup calculate the sum of all text fields keyup计算所有文本字段的总和

  2. Check if the is greater than the Total 检查大于总计

  3. If it is the case Alert and erase the current text field $(this).val(""); 如果是这种情况,则警报并清除当前文本字段$(this).val("");

see fiddle 看小提琴

Update: 更新:

Fiddle 小提琴

Try this. 尝试这个。 Use keyUp event like this way. 像这样使用keyUp事件。

 $( ".marksscored" ).keyup(function() { var marksscored = $(this).val(); var totalmarkstoall = document.getElementById("totalmarkstoall").value; if (parseInt(marksscored) > parseInt(totalmarkstoall)) { alert("greater then Total Mrks"); $(this).val(""); } else { // do something } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <input id="totalmarkstoall" type="number" STYLE=" border: 1px solid #dbdbdb;" placeholder="Enter Total Marks"></input> <table> <tr> <td> <input type="text" class="marksscored" name="marksscored[]" id="marksscored[0]" value="" /> </td> </tr> <tr> <td> <input type="text" class="marksscored" name="marksscored[]" id="marksscored[1]" value="" /> </td> </tr> <tr> <td> <input type="text" class="marksscored" name="marksscored[]" id="marksscored[2]" value="" /> </td> </tr> </table> 

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

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