提交无需验证

时间:2014-11-04 05:36:26

标签: php jquery forms

我在制作简单的在线表格时遇到问题

<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>

此表单有多个提交多个条件的按钮。默认情况下(单击下一个表单按钮),如果您没有回答所有必填字段,则无法提交,但如果您回答了姓名并单击“保存我的数据”按钮,则允许提交,即使作业也会留空

有可能成功吗?请举例说明,以便跳过必填字段

2 个答案:

答案 0 :(得分:2)

在html head标记内添加这些脚本。它会起作用

<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>

答案 1 :(得分: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();
    }
}

希望这有帮助。