2016-11-10 56 views
0

我有2個提交按鈕的窗體上:申請jQuery的表單提交驗證到只有1出2提交按鈕

<div class="text-center"> 
 
    <button type="submit" name="buttonType" value="saveSelected" id="save" class="btn btn-success">Submit</button>&nbsp; &nbsp; 
 
    <button type="submit" name="buttonType" value="declineAll" class="btn btn-danger">Decline All</button> 
 
</div>

我最近添加了一些驗證的表單提交,但我只希望它適用於「提交」按鈕而不是「全部拒絕」按鈕。這裏是我的腳本:

$("form").submit(function(event) { 
 

 
    if ($('#productID').val() == '') { 
 

 
    $("#productID_Error").show(); 
 
    event.preventDefault(); 
 
    return false; 
 

 
    } 
 

 
});

有沒有一種辦法可以限制表單驗證只有火的時候第一個提交按鈕被點擊?

+0

哪裏是你的形式 – Mahi

回答

0

這裏有一些解決方案上點擊事件的形式。

  1. 請改變的衰落的類似屬性的所有按鈕:類型= '按鈕'

  2. 使用以下過程

    $("#save").click(function (e) { 
        // put your validation here 
        $("form").submit(); 
    } 
    
0

您可以更改第二個按鈕類型提交按鈕

<button type="button" name="buttonType" value="declineAll" class="btn btn-danger">Decline All</button> 

使用上面的方法表單驗證只有火的時候第一個提交按鈕被點擊

+0

我仍然需要當他們點擊拒絕所有按鈕提交表單 - 如果我改變的類型「按鈕」它不會出現實際做任何事情或提交表格? – user982124

+0

但你的問題是你想提交表單只提交按鈕不拒絕所有按鈕 –

+0

@ user982124我不認爲你會提交任何東西到服務器端的落選全部按鈕 – ricky

0

你可以改變按鈕類型從submitbutton,見下文代碼

$("form").submit(function(event) { 
 

 
    if ($('#productID').val() == '') { 
 

 
    $("#productID_Error").show(); 
 
    event.preventDefault(); 
 
    return false; 
 

 
    } 
 

 
});
<div class="text-center"> 
 
    <button type="submit" name="buttonType" value="saveSelected" id="save" class="btn btn-success">Submit</button>&nbsp; &nbsp; 
 
    <button type="button" name="buttonType" value="declineAll" class="btn btn-danger">Decline All</button> 
 
</div>

+0

我仍然需要提交表單時,他們點擊拒絕所有按鈕 - 如果我將類型更改爲「按鈕」,它似乎沒有實際做任何事情或提交表單? – user982124

+0

然後你可以使用@RajuMalli建議的第二種方式作爲回答。 – Novice

+1

是的同意你@Novice,這是另一個選項 – Bharat

0
<div class="text-center"> 
     <button type="submit" name="buttonType" value="saveSelected" id="save"  class="btn btn-success" id="submitButton">Submit</button>&nbsp; &nbsp; 
     <button type="submit" name="buttonType" value="declineAll" class="btn btn-danger">Decline All</button> 
</div> 



$("#submitButton").click(function(event) { 
    event.preventDefault(); 
    if ($('#productID').val() == '') { 

    $("#productID_Error").show(); 
    event.preventDefault(); 
    return false; 


} else{ 
    $("#yourForm").submit(); 
    } 
}); 

validatde上提交按鈕

+0

當他們點擊「全部拒絕」按鈕時,我仍然需要提交表單 - 如果我將類型更改爲「按鈕」,它看起來並沒有實際做任何事情或提交形成? – user982124

+0

如果有效,請在button.and submut表單上添加click事件。檢查更新的代碼 – XYZ

0

由於Button type兩個按鈕是相同的,因此衝突。 相反,你可以嘗試從submit改變button type只是button,這種類型的東西:

<div class="text-center"> 
    <button type="submit" name="buttonType" value="saveSelected" id="save" class="btn btn-success">Submit</button>&nbsp; &nbsp; 
    <button type="button" name="buttonType" value="declineAll" class="btn btn-decline">Decline All</button> 
</div> 
0

好像你必須對您的按鈕中的一個點擊事件,你有一個函數內增加一個功能完成您的驗證任務。請檢查下面的代碼 -

HTML

<div class="text-center"> 
    <button type="submit" name="buttonType" value="saveSelected" id="save" class="btn btn-success" onclick="myFunction()">Submit</button>&nbsp; &nbsp; 
    <button type="submit" name="buttonType" value="declineAll" class="btn btn-danger">Decline All</button> 
</div> 

的JavaScript

function myFunction(){ 
    alert("Your validation code"); 
} 

它只會如果按下特定的按鈕來觸發。觸發它的其他按鈕不會被執行。並且不會有任何表單提交問題。

希望它能幫助... :)