簡體   English   中英

對 jQuery 進行表單驗證的最簡單方法是什么?

[英]What's the easiest way to do form validation for jQuery?

我想要做的就是檢查文本字段是否已更改。 如果沒有,我想在按下提交時突出顯示這些框。 我怎么做? 看起來很簡單,但不確定為什么其他所有解決方案都如此復雜。

根據 Pim 的回答,您可以使用 jQuery 的數據 API 為每個文本字段關聯changed標志。

// initially, assign changed to false for all text fields
$("input:text").data("changed", false);

// if any field changes, set its changed flag to true
$("input:text").change(function() {
    $(this).data("changed", true);
}

// finally on submission, get all text fields that did not
//  change by checking their "changed" property
var unchangedItems = $("input:text").filter(function() {
    return $(this).data("changed") === false;
});

您可以使用jQuery 驗證插件。

基本上,你只是說它從來沒有改變過,當它發生變化時,你設置一個標志說 is has been changed:

var changed = false;
$('#textfield').change(function(){
    changed = true;
});
if(changed){
    $('.textbox').each(function(){
        $(this).addClass('.highlighted');
        //or something like this, whatever you want to do to highlight them
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM