0

フォームの必須フィールドを検証しており、クラス "standard_input" をクラス "standard_input_error" に置き換えたいのですが、次のコードが機能しません。また、検証が必要な ID ごとに関数を記述する必要がないように、関数を組み合わせることは可能ですか (検証を必要としないテキスト フィールドがいくつかあるため、フォーム全体に使用することはできません)。 . 前もって感謝します。

$('#firstname').blur(function()
{
    if( !$(this).val() ) {
          $(this).addClass('standard_input_error');
    }
});


Firstname * <input type="text" id="firstname" name="firstname" class="standard_input"> 
Last Name*  <input type="text" id="lastname" name="lastname" class="standard_input">
another   * <input type="text" id="another" name="another" class="standard_input"> 
another 2   <input type="text" id="another2" name="another2" class="standard_input">
Field      * <input type="text" id="fieldb" name="fieldb" class="standard_input"> 
Blah blah   <input type="text" id="blah" name="blah" class="standard_input">

Checkbox 1* <input type="checkbox" checked="checked" id="cbox1" name="cbox1" />
Blah bla 1 <input type="checkbox" checked="checked" id="blabla" name="blabla" />
Blee blee1* <input type="checkbox" checked="checked" id="blee" name="blee" />
bloblo 1   <input type="checkbox" checked="checked" id="blo" name="blo" />
4

3 に答える 3

0

テキストボックスにはこれを使用します:

$('form').find('input[type=text]').each(function(){
    if ($(this).val() == ''){
        $(this).removeClass('standard_input').addClass('standard_input_error');
        return false;
    }
});
于 2012-06-29T10:41:16.607 に答える
0

汎用関数を作成し、それを再利用します。

$('form input[type="text"]').blur(myValidation);

function myValidation() {
  if( !$.trim( this.value) ) {
      $(this).removeClass('standard_input').addClass('standard_input_error');
  } else {
      $(this).removeClass('standard_input_error');
  }
}

デモ

于 2012-06-29T10:45:56.963 に答える
0
$('.validate').bind('blur',function(){
    if (!$(this).val()){
        $(this).removeClass('standard_input');
        $(this).addClass('standard_input_error');
        return false;
    }
});

ここで、「validate」は、検証が必要な入力フィールドに適用されるクラスです

于 2012-06-29T10:48:41.267 に答える