【问题标题】:Validation and compare inputs with jQuery? [closed]使用 jQuery 验证和比较输入? [关闭]
【发布时间】:2013-12-17 22:07:32
【问题描述】:

我有一个输入表。我想比较每一行并显示一条消息。对于消息,我们可以使用 Bootstrap:

foreach..

<td>
  <input type="hidden" value="<?php echo $real_price ?>" id="real_price">
  <input type="text" value="<?php echo $price; ?>" id="price">
</td>

..endforeach

我想将此值与 jQuery 进行比较。

例如: if price &lt; real_price - i 弹出消息“您不能将价格设置为低于实际价格”。也许一个好主意是使用 onchange 事件?

【问题讨论】:

  • 元素的ID必须是唯一的

标签: javascript jquery validation twitter-bootstrap


【解决方案1】:

隐藏字段没用,使用输入字段中的数据属性来存储真实价格,如下所示:

<table>
    <tr>
        <td>
            <input type="text" value="10" data-realprice="11">
            <span></span>
        </td>
    </tr>
    <tr>
        <td>
            <input type="text" value="12" data-realprice="13">
            <span></span>
        </td>
    </tr>
</table>

jQuery 代码下方:

$(document).ready(function () {

    $('input').each(function () {
        var that = $(this);
        if (that.val() < that.data('realprice')) {
            that.next('span').html('You cant set price smaller then real price');
        }
    });
});

【讨论】:

    【解决方案2】:
    <script type="text/javascript">
       $(document).ready(function() {
         var real_price = $("#real_price").val();
         var price = $("#price").val();
         $('#btn').live("click",function(){
            if(real_price < price){
               alert("You cant set price smaller then real price");
               return false;
            }
            else{
               return true;
           }
        });
    });
    

    试试这个

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多