如何在提交包含表格的表单时跳过标题的验证?

时间:2013-02-13 13:04:16

标签: javascript html forms html-table

我有一张表格,里面有一张桌子。我正在做类似

的事情
function validate(){
var result = true;
$("tr").each(function(){

    if($(this).find(".qty").val()<1||isNaN($(this).find(".qty").val())){
        result = false;
        return false;
    }
    if($(this).find(".pvDetails").text()==""){
         result = false;
         return false;
    }
});

if(result)
    return true;
else
    alert('correct the input values | check your ID or Qty field');
return false;

}

但是这也将我的标题用于验证,因为我的标题定义如下 -

<tr height="50px" bgcolor="#EEE8CD">
                    <th align="left" width="10%">Serial No.</th>
                    <th align="left" width="15%">Variant ID</th>
                    <th align="left" width="20%">Product Detail</th>
                    <th align="left" width="17%">Product Image</th>
                    <th align="left" width="10%">B2B Price</th>
                    <th align="left" width="13%">Quantity</th>
                    <th align="left" width="12%">Total</th>
                </tr>

休息是我的桌子。

如何使用与我一样的方法跳过表头的验证。

2 个答案:

答案 0 :(得分:1)

我认为标题的标记名称仅为<th>,而您的表格内容单元格为<td>,因此基本的想法是检查<tr> <的子标记名称/ p>

   $("tr").children().each(function () {
    if ($(this).prop("tagName") == "td") {
        if ($(this).find(".qty").val() < 1 || !isNaN($(this).find(".qty").val())) {
            result = false;
            return false;
        }
        if ($(this).find(".pvDetails").text() == "") {
            result = false;
            return false;
        }
    }
   });

好吧,另一个想法就是只在<tbody>部分阅读就像这样

$("#mytable tbody tr").children().each(function () {

        if ($(this).find(".qty").val() < 1 || !isNaN($(this).find(".qty").val())) {
            result = false;
            return false;
        }
        if ($(this).find(".pvDetails").text() == "") {
            result = false;
            return false;
        }
   });

答案 1 :(得分:1)

将“skipvalidation”的类名添加到您不想验证的任何行。然后将第3行修改为:

$("tr").not('.skipvalidation').each(function(){ ...