0

我遇到了问题...当用户单击提交时-显示错误消息,但 jQuery 似乎并没有在 Return False 上停止;

请参见下面的代码:

    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();
        if(qty1 === '' && selectVal != 'Please Select...') {
            alert("You've chosen an option, but not entered a quantity to dispute, please check your inputs.");
            return false;           
        }
        if(qty1 != '' && selectVal === 'Please Select...') {
            alert("You've entered a quantity, but not chosen why, please check your reasons.");
            return false;           
        }
        if (qty1 > qty2) {
            alert("For one of your entries, the disputed quantity is larger than the shipped quantity.");
            return false;           
        }
   });   
}

调用它的 HTML

<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 validateSubmit();\">";

                            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']." name = \"ship$i\" 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>

有任何想法吗??非常感谢帮助

4

4 回答 4

2

当前的返回将离开each(),而不是validateSubmit()(validateSubmit 目前不返回任何东西)

在 validateSubmit() 的开头定义一个变量,例如

var r=true;//default returnValue

并将其放在 validateSubmit() 的末尾:

return r;

现在,当您想离开 validateSubmit() 时,请在每个内部调用:

r=false;return;

这将留下each()validateSubmit()返回值r(现在将是什么false

于 2013-01-20T00:12:29.127 回答
0

向您的代码添加preventDefault调用。换行:

function validateSubmit(){

到以下

function validateSubmit(e){
    e.preventDefault();
    ...
于 2013-01-19T23:57:34.727 回答
0

确保您的按钮也使用 'return' 来返回返回值:

 <input type="submit" onClick="return validateSumbit();" />

此外,您应该从表单标签中删除 onsubmit 处理程序。

于 2013-01-20T00:07:15.850 回答
0

您的validateSubmit()函数似乎没有返回值。只有each()方法的回调函数返回一个值,但您的实际提交处理程序没有。本质上,您的验证代码按预期运行,显示错误消息但不返回任何值以指示失败。

validateSubmit()您应该在each()方法可以修改和返回的范围内定义一个变量。

例如:

 function validateSubmit() {
     var form_is_valid = true;

     $('tr').each( function() {
         // Validation goes here
         if (something_is_invalid) {
             form_is_valid = false;
             return;
         }
     });

     return form_is_valid;
 }
于 2013-01-20T00:19:16.080 回答