2014-11-04 100 views
-1

我有問題作簡單的在線形式提交未經驗證

<form method="post"> 
<input type="text" name="name" required> 
<input type="text" name="job" required> 
<input type="submit" name="save" value="Save My Data"> 
<input type="submit" name="next" value="Next Form"> 
</form> 

這種形式有多個按鈕與多個條件提交。默認情況下(單擊下一個表單按鈕),如果您未回答所有必填字段,則無法提交,但如果您回答了姓名並單擊保存我的數據按鈕,則您允許提交,即使作業也保留爲空白

是否有可能使?請給我示例,以使所需的字段可以跳過

+0

yup..checkout'ajax' – 2014-11-04 05:39:11

回答

0

對於這種嘗試給ID添加到輸入的字段,然後從JavaScript訪問它們即

HTML:

<form method="post"> 
<input type="text" name="name" required id="name"> 
<input type="text" name="job" required id="job"> 
<input type="submit" name="save" value="Save My Data" id="savebtn"> 
<input type="submit" name="next" value="Next Form" id="nextbtn"> 
</form> 

現在腳本:

$("#savebtn").click(e){ 
    e.preventDefault(); 
    if($("#name").length>0 || $("#job").lenght>0){ 
     $(this).submit(); 
    } 
} 

$("#nextbtn").click(e){ 
    e.preventDefault(); 
    if($("#name").length>0 && $("#job").lenght>0){ 
     $(this).submit(); 
    } 
} 

希望這會有所幫助。

2

將這些腳本添加到您的html頭標記中。它將工作

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

<script> 
    $(document).ready(function(){ 
     $('input[name="job"]').attr('required'); 
     $('input[name="save"]').click(function(){ 
        $('input[name="job"]').removeAttr('required'); 
     }); 
     $('input[name="next"]').click(function(){ 
        $('input[name="job"]').attr('required','required'); 
     }); 
    }); 
</script>