2013-05-28 56 views
0

這裏驗證表單是我的代碼:使用jQuery.click不起作用

$('input#price_match_submit').click(function(event) { 
     if ($.trim($('input#price_match_competitor_price').val()) == '') { 
      alert("Please enter competitor's price."); 
      return false; 
     } 
     if ($.trim($('input#price_match_name').val()) == '') { 
      alert("Please enter your name."); 
      return false; 
     } 
     if ($.trim($('input#price_match_quantity').val()) == '') { 
      alert("Please enter the quantity."); 
      return false; 
     } 
     if ($.trim($('input#price_match_email').val()) == '') { 
      alert("Please enter your email address."); 
      return false; 
     } 
     if ($.trim($('input#price_match_competitor_website').val()) == '') { 
      alert("Please enter competitor's website."); 
      return false; 
     } 
     if ($.trim($('input#price_match_phone_number').val()) == '') { 
      alert("Please enter your phone number."); 
      return false; 
     } 
    }); 

這裏#price_match_submit是一個提交按鈕。當我點擊按鈕時,這個函數應該執行並驗證表單。但它沒有按照我的預期工作。該表格未經任何驗證即被提交。我在哪裏做錯了?

+0

請顯示您的'HTML',以便我們可以測試它。小提琴將幫助我們解決您的問題。 –

+0

你可以請發佈按鈕代碼? –

+0

點擊功能將無法正常工作,因爲它不會阻止默認提交。所以使用提交而不是點擊。 – Rajiv007

回答

-1

謝謝你的回答和評論。

此代碼正常工作。代碼的其他部分存在一些問題。

當我們將一個函數分配給一個元素時,該函數只被分配給該物理元素。但是當我們對元素進行一些物理修改時,它之前的所有屬性都會丟失。在我的代碼中,我在模態彈出窗口中顯示了這個html。這是複製html而不是使用相同的元素。所以,它失去了與該功能的綁定。這就是這段代碼無法工作的原因。

0

你可能反而要掛接到父窗體的提交事件,並需要防止默認行爲:

$('#form').on('submit', function (e) { 
    if (everything_failed) { 
     e.preventDefault(); 
     return false; 
    } 
}); 

返回FALSE;只停止我認爲的事件冒泡。

0

您可以驗證這樣

$('form').on('submit', function() { 
// do validation here 
     if ($.trim($('input#price_match_competitor_price').val()) == '') { 
     alert("Please enter competitor's price."); 
     return false; 
    } 
    if ($.trim($('input#price_match_name').val()) == '') { 
     alert("Please enter your name."); 
     return false; 
    } 
    if ($.trim($('input#price_match_quantity').val()) == '') { 
     alert("Please enter the quantity."); 
     return false; 
    } 
    if ($.trim($('input#price_match_email').val()) == '') { 
     alert("Please enter your email address."); 
     return false; 
    } 
    if ($.trim($('input#price_match_competitor_website').val()) == '') { 
     alert("Please enter competitor's website."); 
     return false; 
    } 
    if ($.trim($('input#price_match_phone_number').val()) == '') { 
     alert("Please enter your phone number."); 
     return false; 
    } 
}); 

返回false,如果不進行驗證。

0
$(#price_match_submit').click(function(event) { 
    if ($.trim($('input#price_match_competitor_price').val()) == '') { 
     alert("Please enter competitor's price."); 
     event.preventDefault(); 
    } 
    else if ($.trim($('input#price_match_name').val()) == '') { 
     alert("Please enter your name."); 
     event.preventDefault(); 
    } 
    else if ($.trim($('input#price_match_quantity').val()) == '') { 
     alert("Please enter the quantity."); 
     event.preventDefault(); 
    } 
    else if ($.trim($('input#price_match_email').val()) == '') { 
     alert("Please enter your email address."); 
     event.preventDefault(); 
    } 
    else if ($.trim($('input#price_match_competitor_website').val()) == '') { 
     alert("Please enter competitor's website."); 
     event.preventDefault(); 
    } 
    else if ($.trim($('input#price_match_phone_number').val()) == '') { 
     alert("Please enter your phone number."); 
     event.preventDefault(); 
    } 

    // if nothing happened until now, the form will be submited 
});