2014-01-29 69 views
0

我在這裏有兩個不同的jQuery。用於更改rfq和投標文本框值的表單驗證和腳本取決於下拉選擇的值。我的第二個腳本功能,當我的下拉選擇例如出價,出價文本框顯示和rfq文本框隱藏,它的值是明確的。就像如果我選擇rfq rfq文本框出來和投標文本框隱藏,它的價值變得清晰。問題是,當我嘗試提交時,表單驗證出現bcoz rfq或投標文本框的值爲none。混合兩個不同的腳本

這裏是我的小提琴http://jsfiddle.net/mHCk7/

我需要的是我的第二腳本混合,以我的第一個腳本才能正常運行我的驗證。

請幫忙嗎?

1腳本

jQuery(function($) { 
    var validation_holder; 

    $("form#register_form input[name='submit']").click(function() { 

    var validation_holder = 0; 
// /^[A-Za-z\s]+$/ 


     var rfq   = $("form#register_form input[name='n_rfq']").val(); 
     var rfq_regex = /^[0-9\-]+$/; // reg ex qty check 
     var bid    = $("form#register_form input[name='n_bid']").val(); 
     var bid_regex = /^[0-9\-]+$/; // reg ex qty check 
     var mode    = $("form#register_form select[name='n_mode']").val(); 
     var mode_regex  = /^[a-zA-Z ]+$/; // reg ex qty check 

     /* validation start */ 

     if(bid == "") { 
      $("span.val_bid").html("This field is required.").addClass('validate'); 
      validation_holder = 1; 
     } else { 
      if(!bid_regex.test(bid)){ // if invalid phone 
       $("span.val_bid").html("Integer Only is Allowed!").addClass('validate'); 
       validation_holder = 1; 

      } else { 
       $("span.val_bid").html(""); 
      } 
     } 

     if(rfq == "") { 
      $("span.val_rfq").html("This field is required.").addClass('validate'); 
      validation_holder = 1; 
     } else { 
      if(!rfq_regex.test(rfq)){ // if invalid phone 
       $("span.val_rfq").html("Integer Only is Allowed!").addClass('validate'); 
       validation_holder = 1; 

      } else { 
       $("span.val_rfq").html(""); 
      } 
     } 

     if(mode == "") { 
      $("span.val_mode").html("This field is Required.").addClass('validate'); 
      validation_holder = 1; 
     } else { 
      if(!mode_regex.test(mode)){ // if invalid phone 
       $("span.val_mode").html("Invalid Special Characters!").addClass('validate'); 
       validation_holder = 1; 

      } else { 
       $("span.val_mode").html(""); 
      } 
     }   

     if(validation_holder == 1) { // if have a field is blank, return false 
      $("p.validate_msg").slideDown("fast"); 
      return false; 
     } validation_holder = 0; // else return true 
     /* validation end */  
    }); // click end 

}); // jQuery End 

第二腳本

$('#txt1').change(function() { 
if ($(this).val() == 'NEGOTIATED' || $(this).val() == 'SHOPPING' || $(this).val() == '') { 
    $("#txt2,#txt3").val(''); 
} 
else if ($(this).val() == 'BIDDING') { 
    $("#txt3").val(''); 
} 
else if ($(this).val() == 'RFQ') { 
    $("#txt2").val(''); 
} 
else { 
    //here you can specify what to do if the value is NOT negotiated or SHOPPING 
} 
}); 

回答

0

/**改變這樣的**/

/*在頁文本框控件添加類名作爲強制性*/

if(bid.hasClass("mandatory")) { 
     $("span.val_bid").html("This field is required.").addClass('validate'); 
     validation_holder = 1; 
    } else { 
     if(!bid_regex.test(bid)){ // if invalid phone 
      $("span.val_bid").html("Integer Only is Allowed!").addClass('validate'); 
      validation_holder = 1; 

     } else { 
      $("span.val_bid").html(""); 
     } 
    } 

    if(rfq.hasClass("mandatory")) { 
     $("span.val_rfq").html("This field is required.").addClass('validate'); 
     validation_holder = 1; 
    } else { 
     if(!rfq_regex.test(rfq)){ // if invalid phone 
      $("span.val_rfq").html("Integer Only is Allowed!").addClass('validate'); 
      validation_holder = 1; 

     } else { 
      $("span.val_rfq").html(""); 
     } 
    } 

/**第二腳本**/

$('#txt1').change(function() { 
if ($(this).val() == 'NEGOTIATED' || $(this).val() == 'SHOPPING' || $(this).val() == '') { 
    $("#txt2,#txt3").val(''); 
    $("#txt2").removeClass("mandatory"); 
    $("#txt3").removeClass("mandatory"); 
} 
else if ($(this).val() == 'BIDDING') { 
    $("#txt3").val(''); 
    $("#txt3").removeClass("mandatory"); 
} 
else if ($(this).val() == 'RFQ') { 
    $("#txt2").val(''); 
    $("#txt2").removeClass("mandatory"); 
} 
else { 
    //here you can specify what to do if the value is NOT negotiated or SHOPPING 
} 
}); 
+0

我現在嘗試。 – user3097736

+0

可以試着把它變成小提琴嗎?我嘗試過,但它不工作 – user3097736