如何检查一个值是否大于其他值

时间:2022-08-24 16:28:41

I have some problems with this checking in jQuery.

我在jQuery中检查时遇到了一些问题。

Problem:

I have dynamically generated table. In this table I have many rows. In each row I have checkbox and input. If input is not empty then checkbox checked automatically on input blur.

我有动态生成的表。在这张表中我有很多行。在每一行中我都有复选框和输入。如果输入不为空,则在输入模糊时自动选中复选框。

e.g. I have two filled inputs with some values and checkboxes are checked also. I want to check if the input value is greater then some value.

例如我有两个填充输入和一些值,也检查了复选框。我想检查输入值是否大于某个值。

Code:

$("#btn_send_order")
  .on("click", function(){

  var keys = new Array(),
      array = new Array();

  $(':checkbox[class="send_vacc checkbox"]:checked').each (function() 
  {
    if ( parseInt($("input[id='"+this.id+"'].sended_cnt").val()) > parseInt($("input[id='"+this.id+"'].sended_cnt").attr("class").split(" ")[2]) )
    {
      return false;
    }
    else
    {
      array.push( $("input[id='"+this.id+"'].sended_cnt").val() );
      keys.push( this.id );
    }
  });
});

So, I have a value which is entered into input and second value I get from its class which is formed from a php script.

所以,我有一个输入到输入的值和我从它的类中获得的第二个值,它是由php脚本组成的。

Code above only works if the first input has greater value then its class value (I test this code on two rows table)

上面的代码仅在第一个输入的值大于其类值时才起作用(我在两行表上测试此代码)

<tbody>
    <tr style="text-align: center; background-color: pink;" class="rek888889" id="76">
        <td>1</td>
        <td>ser5555778</td>
        <td>test_vaccine98</td>
        <td>74 / <input type="text" id="76" name="sended_cnt" class="sended_cnt rek888889 74" maxlength="3"></td>
        <td>31.10.2026</td>
        <td>rek888889</td>
        <td><input class="send_vacc checkbox" id="76" type="checkbox"></td>
    </tr>
    <tr style="text-align: center; background-color: pink;" class="rek123" id="1">
        <td>2</td>
        <td>ser1098</td>
        <td>test_vaccine_1</td>
        <td>600 / <input type="text" id="1" name="sended_cnt" class="sended_cnt rek123 600" maxlength="3"></td>
        <td>13.10.2025</td>
        <td>rek123</td>
        <td><input class="send_vacc checkbox" id="1" type="checkbox"></td>
    </tr>
</tbody>

How can I change this code to check all the values?

如何更改此代码以检查所有值?

1 个解决方案

#1


2  

You can't have same ID more than once in your code. So checkbox and input IDs must be different. I renamed input IDs to inputXX . Also you could write your selectors in much better ways.

您的代码中不能多次使用相同的ID。因此复选框和输入ID必须不同。我将输入ID重命名为inputXX。您也可以用更好的方式编写选择器。

fiddle

https://jsfiddle.net/ergec/juney6mq/

snippet

$("#btn_send_order").on("click", function() {
    var keys = new Array(),
        array = new Array();
    $('.send_vacc.checkbox:checked').each(function() {
        if (parseInt($("input[id='input" + this.id + "'].sended_cnt").val()) > parseInt($("input[id='input" + this.id + "'].sended_cnt").attr("class").split(" ")[2])) {
            return false;
        } else {
            array.push($("input[id='input" + this.id + "'].sended_cnt").val());
            keys.push(this.id);
        }
    });
	console.log(keys);
	console.log(array);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
    <tbody>
        <tr style="text-align: center; background-color: pink;" class="rek888889" id="76">
            <td>1</td>
            <td>ser5555778</td>
            <td>test_vaccine98</td>
            <td>74 /
                <input type="text" id="input76" name="sended_cnt" class="sended_cnt rek888889 74" maxlength="3">
            </td>
            <td>31.10.2026</td>
            <td>rek888889</td>
            <td>
                <input class="send_vacc checkbox" id="76" type="checkbox">
            </td>
        </tr>
        <tr style="text-align: center; background-color: pink;" class="rek123" id="1">
            <td>2</td>
            <td>ser1098</td>
            <td>test_vaccine_1</td>
            <td>600 /
                <input type="text" id="input1" name="sended_cnt" class="sended_cnt rek123 600" maxlength="3">
            </td>
            <td>13.10.2025</td>
            <td>rek123</td>
            <td>
                <input class="send_vacc checkbox" id="1" type="checkbox">
            </td>
        </tr>
    </tbody>
</table>
<button id="btn_send_order">Send Order
</button>

#1


2  

You can't have same ID more than once in your code. So checkbox and input IDs must be different. I renamed input IDs to inputXX . Also you could write your selectors in much better ways.

您的代码中不能多次使用相同的ID。因此复选框和输入ID必须不同。我将输入ID重命名为inputXX。您也可以用更好的方式编写选择器。

fiddle

https://jsfiddle.net/ergec/juney6mq/

snippet

$("#btn_send_order").on("click", function() {
    var keys = new Array(),
        array = new Array();
    $('.send_vacc.checkbox:checked').each(function() {
        if (parseInt($("input[id='input" + this.id + "'].sended_cnt").val()) > parseInt($("input[id='input" + this.id + "'].sended_cnt").attr("class").split(" ")[2])) {
            return false;
        } else {
            array.push($("input[id='input" + this.id + "'].sended_cnt").val());
            keys.push(this.id);
        }
    });
	console.log(keys);
	console.log(array);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
    <tbody>
        <tr style="text-align: center; background-color: pink;" class="rek888889" id="76">
            <td>1</td>
            <td>ser5555778</td>
            <td>test_vaccine98</td>
            <td>74 /
                <input type="text" id="input76" name="sended_cnt" class="sended_cnt rek888889 74" maxlength="3">
            </td>
            <td>31.10.2026</td>
            <td>rek888889</td>
            <td>
                <input class="send_vacc checkbox" id="76" type="checkbox">
            </td>
        </tr>
        <tr style="text-align: center; background-color: pink;" class="rek123" id="1">
            <td>2</td>
            <td>ser1098</td>
            <td>test_vaccine_1</td>
            <td>600 /
                <input type="text" id="input1" name="sended_cnt" class="sended_cnt rek123 600" maxlength="3">
            </td>
            <td>13.10.2025</td>
            <td>rek123</td>
            <td>
                <input class="send_vacc checkbox" id="1" type="checkbox">
            </td>
        </tr>
    </tbody>
</table>
<button id="btn_send_order">Send Order
</button>