0

関数の戻り値について少し混乱しています。フォームがあり、その中にテキスト ボックスとラジオ ボタンがあります。すべてのテキスト ボックスとラジオ ボタンをまとめて検証しています。したがって、これら2つの関数から2つの戻り値があります。これら2つの関数に同じ戻り変数を使用できますか? これは私のコードです

$(function() {
$('#submitBtn').click(function(){
    var returnValue = true;
    //Validating radio buttons required
    //Code of checking radio buttons
        if( !unchecked.is(':checked') ) {
            alert("Required field");
            returnValue = false;
        }
    });

    //Other Required fields

    $('.required_text').filter(':visible').each(function () {
        var input = $(this);
        if (!$(this).val()) {
            alert("Required field");
            returnValue = false;
        }
    });
    alert(returnValue);
    return returnValue;
});

}
});

radioReturn や textReturn のようにこれらの関数ごとに異なる戻り変数を使用し、最後に使用する場合

if(radioReturn && textReturn){
returnValue = true;
}
else{
returnValue = false;
}

しかし、あまりにも多くの変数を使用したくありません。したがって、1 つの戻り変数のみを使用してフォームの送信を処理できる方法はありますか。

ありがとう

4

1 に答える 1

0

はい、できます

このようにしてみてください

$(function() {
    // all validation inside the click event
    $('#submitBtn').click(function(){
      // initiate the value as true
      var returnValue = true;
      //Validating radio buttons required
      //Code of checking radio buttons
      if( !unchecked.is(':checked') ) {
        alert("Required field");
        // add css to highlight error
        returnValue = false;
      }
      // check the input fields only if returnValue is true
      // if u want to highlight  all the errors then this check no need
      if(returnValue)
      {
        $('.required_text').filter(':visible').each(function () {
           var input = $(this);
           if (!$(this).val()) {
              // add css to highlight error
              alert("Required field");
           returnValue = false;
           }
         });
      }
      //Other Required fields

      alert(returnValue);
      return returnValue;
});


});

更新 2:

  $('#submitBtn').click(function(){
       // initiate the value as true
       var returnValue = true;


       //Validating radio buttons required
       //Code of checking radio buttons
       if( !unchecked.is(':checked') ) {             
         // add css to highlight error or
         // add label nearby it to said Required
         returnValue = false;
        }

        // check the input fields only if returnValue is true
        // if u want to highlight  all the errors then this check no need
        $('.required_text').filter(':visible').each(function () {
          var input = $(this);
          if (!$(this).val()) {
             // add css to highlight error or
             // add label nearby it to said Required
           returnValue = false;
        }
      });

      //Other Required fields

      // here if u have any errors then the return value always false
      alert(returnValue);
      return returnValue;
  });
于 2013-11-13T13:46:56.180 に答える