2011-08-26 210 views
0
$('#err input').bind('blur keyup',function() { 
     $(this).data('errors', "ABC " + $(this).data('err') + " " + $(this).val()); 
    }); 

$('#go').click(function(){ 
    var code = ""; 
    $("#err input").each(function(){ 
    code += $(this).data('errors') + "\n" || ''; 
    }); 
    $('#x').html(code); 
}); 

HTML:jQuery驗證輸入

<div id="err"> 
    <input data-err="A" id="e-1" type="text"/> 
    <input data-err="B" id="e-2" type="text"/> 
    <input data-err="C" id="e-3" type="text"/> 
</div> 

如何進行驗證,如果輸入將是空的,那麼這個輸入不將被處理?

回答

1

試試這個

$('#go').click(function(){ 
    var code = ""; 
    $("#err input").each(function(){ 
    //This condition will check for empty values 
    if($(this).val()){ 
     code += $(this).data('errors') + "\n" || ''; 
    } 

    }); 
    $('#x').html(code); 
}); 
+0

作品,謝謝:) –

0

從提交處理程序返回false會取消默認表單提交功能。因此,使用:

$('#go').submit(function(){ 
    //do validation here 
    if(validationFails)return false; 
});