检查文本框的编号是否大于输入的数字文本框数

时间:2015-07-14 08:30:58

标签: javascript jquery html

我正在尝试将单个文本框值" totalmarkstoall" 的值与多个文本框" marksscored&#34进行比较; ,下面我的java脚本也在关键功能上进行比较。

无法做到的是,如果" marksscored" 的值比perticuler文本框大,那么" 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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

如果您执行此类操作,则可以知道该值来自哪个输入字段。

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:

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 {

   }
 }

答案 1 :(得分:0)

你可以使用jquery做这样的事情:

  1. keyup上计算所有文本字段的总和

  2. 检查是否大于总计

  3. 如果是这种情况警告并删除当前文本字段$(this).val("");

  4. see fiddle

    更新

    Fiddle

答案 2 :(得分:0)

试试这个。像这样使用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>