使用jQuery检查一个字段与另一个字段

时间:2013-01-18 20:50:20

标签: php html sql

在编写订单提交页面时,我遇到了一个很大的问题,页面的目的是为订单提交争议 - 提供两个字段,但只有当一个字段少于另一个字段时才会填写。< / p>

基本上,一个是下拉列表,另一个是争议框,查询如下:

如果DisputesTextBox =“”并且下拉框=“请选择...”

一切都很好 - 启用提交按钮

如果DisputesTextBox!=“”和下拉框=“请选择...”

错误(反之亦然,因此如果填充了争议但dropwn =请选择...) - 禁用提交按钮

如果DisputesTextox!=“”和下拉框=“其他”

一切都很好 - 启用提交按钮

如果DisputesTextBox&gt; shippingbox

错误 - 禁用提交按钮

<table>
                <thead>
                    <tr><th>Item ID</th><th>Description</th><th>Dispute Quantity</th><th>Shipped Quantity</th><th>Ordered Quantity</th><th>Reason</th></tr>
                </thead>
                <tbody>
                    <?php
                        $data = mysql_query("SELECT * FROM `artran09` WHERE `invno` = '$invoiceno'") or die(mysql_error());
                        echo "<center>";
                        $i = -1;        
                        echo "<form action=\"submitdispute.php?invno=".$invoiceno."&ordate=".$placed."\" method=\"POST\" onsubmit=\"return confirm('Are you sure you are ready to dispute your order?');\">";

                            while ($info = mysql_fetch_array($data)) {              
                                $i += 1;                                    
                                echo "<tr>"; 
                                echo "<td>".$info['item']."</td>"; 
                                echo "<td>".$info['descrip']."</td>";       

                                echo "<td><input type=\"text\" input name=".$i." onKeyPress=\"return numbersonly(this, event)\"  maxLength=\"3\"></td>"; 

                                echo "<td><input type=\"text\" value=".$info['qtyshp']." onKeyPress=\"return numbersonly(this, event)\" maxLength=\"3\" disabled=\"disabled\"></td>"; 

                                echo "<td><input type=\"text\" value=".$info['qtyord']." onKeyPress=\"return numbersonly(this, event)\" maxLength=\"3\" disabled=\"disabled\"></td>"; 

                                echo "<td><select name = \"reason$i\">";
                                echo "<option>Please Select...</option>";
                                echo "<option>Short/Not received</option>";
                                echo "<option>Damaged Goods</option>";
                                echo "<option>Product Not Ordered</option>";                    
                                echo "</select></td>";

                                echo "</tr>"; 
                            }

                    ?>
                </tbody>
            </table>
        </div>
    </div>
    <p><input type = "submit" value = "Dispute" name ="Submit">
    </form>

谢谢,希望任何人都可以提供帮助!

为Wolf /任何可以提供帮助的人编辑 - 这里的代码是什么:

- 我编辑的代码 -

function validateSubmit(){


   // this will loop through each row in table
   // Make sure to include jquery.js
   $('tr').each( function() {
      // Find first input
      var input1 = $(this).find('input').eq(0);
      var qty1 = input1.val();
      // Find Second input
      var input2 = $(this).find('input').eq(1);
      var qty2 = input2.val();
      // Find third input
      var input3 = $(this).find('input').eq(2);
      var qty3 = input3.val();
      // Find select box
      var selectBx = $(this).find('select');
      var selectVal = selectBx.val();
      // Add your validation code here for the inputs and select
      // Return true if all validations are correct
      // Else return false
        if(qty1 = "" && selectVal = "Please Select...") {
            return true;
            alert("You have an error somewhere, please check over your quantites.");
            break;
        }
        if (qty1 > qty2) {
            return false;
            alert("You have an error somewhere, please check over your quantites.");
            break;
        }
   });


}

1 个答案:

答案 0 :(得分:0)

更改以下行

onsubmit=\"return confirm('Are you sure you are ready to dispute your order?');\"

onsubmit=\"return validateSubmit();\"

然后编写函数

function validateSubmit(){

   // this will loop through each row in table
   // Make sure to include jquery.js
   $('tr').each( function() {
      // Find first input
      var input1 = $(this).find('input').eq(0);

      // Find Second input
      var input2 = $(this).find('input').eq(1);

      // Find third input
      var input3 = $(this).find('input').eq(2);

      // Find select box
      var selectBx = $(this).find('select');

      // Add your validation code here for the inputs and select
      // Return true if all validations are correct
      // Else return false

   });
}

请注意,您的提交按钮将一直处于启用状态。但只有在验证成功的情况下,提交才会有效。