2017-06-02 100 views
0

我有4個按鈕和4個文本框,我正在驗證文本框使用引導 和問題是形式不應該提交如果按鈕沒有點擊現在我怎樣才能驗證按鈕如何防止表單提交如果按鈕沒有點擊形式

<form id="test"> 
     <form id="test">           
      <table class="table table-bordered display nowrap" > 
       <thead> 
        <tr> <th >Button</th> 
          <th >Textbox</th> 
         </tr> 
       </thead> 
       <tbody> 
        <tr> 
         <td> 
          <button type="button" class="btn btn-primary btn2">button1</button> 
         </td> 
         <td> 
          <input type="text" class="form-control" name="Allowances"> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <button type="button" class="btn btn-primary btn2">button2</button> 
         </td> 
         <td> 
          <input type="text" class="form-control" name="Allowances"> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <button type="button" class="btn btn-primary btn2">button3</button> 
         </td> 
         <td> 
          <input type="text" class="form-control" name="Allowances"> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <button type="button" class="btn btn-primary btn2">button4</button> 
         </td> 
         <td> 
          <input type="text" class="form-control" name="Allowances"> 
         </td> 
        </tr>           
      </table>  
       <input type="submit" class="btn btn-info" > 
     </form> 

引導驗證

$('#test').bootstrapValidator(
      { 
       message : 'This value is not valid', 
       feedbackIcons : { 
        validating : 'glyphicon glyphicon-refresh' 
       }, 

       fields : { 



        'Allowances': { 
         validators : { 
          notEmpty : { 
           message : 'Enter values' 
          } 
         } 
        }, 



       } 

      }) 
    } 

); 
+0

你的按鈕有'型= button'所以他們不應該提交。你的網頁有錯誤嗎?看起來你的JS有一些錯誤。 – putvande

+0

我沒有任何錯誤,我的問題是形式不應該提交如果按鈕沒有點擊 – Priya

+0

你試過使用'''event.preventDefault();'''? –

回答

0

你可以試試這個。我已經測試here

$("form#test button").on("click", function(){ 
 
    console.log(this); 
 
    $(this).addClass("clicked"); \t \t 
 
}); 
 

 
$("#submitButton").click(function(event){ 
 
    var clickedButtonCount = $("form#test button.clicked").length; 
 
    var buttonCount = $("form#test button").length; 
 
    if(clickedButtonCount == buttonCount) 
 
    { 
 
     $("#test").submit() 
 
     alert("Submitted!"); 
 
    } 
 
    else 
 
    { 
 
     alert("Pleas click all buttons"); 
 
    } 
 
});

+0

代碼未運行 – Priya

+0

是有任何其他方法 – Priya

+0

有我使用迭代器生成按鈕我不知道有多少按鈕生成,所以我不能使用btn2 = false – Priya

0

試試這個,它工作正常:

var btn1= false 
 
var btn2= false 
 
var btn3= false 
 
var btn4= false 
 
$("#test").click(function(event){ 
 
    event.preventDefault(); 
 
}); 
 
$("#button1").click(function() { 
 
    btn1 = true 
 
}) 
 
$("#button2").click(function() { 
 
    btn2 = true  
 
}) 
 
$("#button3").click(function() { 
 
    btn3 = true  
 
}) 
 
$("#button4").click(function() { 
 
    btn4 = true  
 
}) 
 
$("#submitButton").click(function(event){ 
 
    if (btn1 == true && btn2 == true && btn3 == true && btn4 == true){ 
 
     $("#test").submit() 
 
     alert ("Submitted!") 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!-- Latest compiled and minified CSS --> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
 

 
<!-- Optional theme --> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> 
 

 
<!-- Latest compiled and minified JavaScript --> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 
 
    <form id="test">           
 
     <table class="table table-bordered display nowrap" > 
 
      <thead> 
 
       <tr> <th >Button</th> 
 
         <th >Textbox</th> 
 
        </tr> 
 
      </thead> 
 
      <tbody> 
 
       <tr> 
 
        <td> 
 
         <button type="button" class="btn btn-primary btn2" id="button1">button1</button> 
 
        </td> 
 
        <td> 
 
         <input type="text" class="form-control" name="Allowances"> 
 
        </td> 
 
       </tr> 
 
       <tr> 
 
        <td> 
 
         <button type="button" class="btn btn-primary btn2" id="button2">button2</button> 
 
        </td> 
 
        <td> 
 
         <input type="text" class="form-control" name="Allowances"> 
 
        </td> 
 
       </tr> 
 
       <tr> 
 
        <td> 
 
         <button type="button" class="btn btn-primary btn2" id="button3">button3</button> 
 
        </td> 
 
        <td> 
 
         <input type="text" class="form-control" name="Allowances"> 
 
        </td> 
 
       </tr> 
 
       <tr> 
 
        <td> 
 
         <button type="button" class="btn btn-primary btn2" id="button4">button4</button> 
 
        </td> 
 
        <td> 
 
         <input type="text" class="form-control" name="Allowances"> 
 
        </td> 
 
       </tr>           
 
     </table>  
 
      <input type="submit" id="submitButton" class="btn btn-info" > 
 
    </form>

+0

謝謝你的答案,但問題是所有的按鈕都有相同的ID,因爲我通過迭代器獲取此按鈕 – Priya

+1

@Priya多個按鈕具有相同的ID是一個壞主意 –

相關問題