2014-11-04 143 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>