jQuery:表单没有提交$('#id')。submit();,但是会提交一个'submit'按钮?

时间:2014-05-27 06:42:09

标签: javascript php jquery

当我通过输入类型按钮提交表单时,我遇到了问题,这是我的代码:

<form method="POST" id="eulaForm" action="downloadPack.php?packId=<?php echo $_REQUEST['packId'];?>" enctype="multipart/form-data">

这是我的表格,对我来说很好看。在那种形式,我把这个按钮:

<input name="declineBtn" id="declineBtn" value="Decline" type="button" class="back">

        <script type="text/javascript">

                $(document).ready(function(){
                    $('#declineBtn').click( function() {
                        searchTxt = $('#searchText').val();
                        // ajax logic to test for what you want
                        if (searchText != '') { return chgAction(searchTxt); } else { return true;}
                    });
                });

                function chgAction(cmpText)
                {
                    if(cmpText != '')
                        {
                            $('#eulaForm').attr('action', 'searchResult.php');
                            alert("After - action = "+$("#eulaForm").attr("action"));
                            //submit the form
                            $('#eulaForm').submit();                    
                        }
                    else{
                            url = '<?php 
                                    echo basename($_SERVER['HTTP_REFERER']);?>';                            
                            window.parent.location.href = url;  
                                    //parent.history.back();
                            return false;
                        }
                }


       </script>

如您所见,它调用$('#eulaForm').submit();,但表单未提交(即页面不刷新)。那是为什么?

谢谢!

1 个答案:

答案 0 :(得分:-1)

尝试这样: 准备好文档中的chgAction()并添加preventDefault()

               $(document).ready(function(){
                    $('#declineBtn').click( function(e) {
                        e.preventDefault();
                        searchTxt = $('#searchText').val();
                        // ajax logic to test for what you want
                        if (searchText != '') { return chgAction(searchTxt); } 
                       else {  return true;}
                    });


                function chgAction(cmpText)
                {
                    if(cmpText != '')
                        {
                            $('#eulaForm').attr('action', 'searchResult.php');
                            alert("After - action = "+$("#eulaForm").attr("action"));
                            //submit the form
                            $('#eulaForm').submit();   
                            return false;                 
                        }
                    else{
                            url = '<?php 
                                    echo basename($_SERVER['HTTP_REFERER']);?>';                            
                            window.parent.location.href = url;  
                                    //parent.history.back();
                            return false;
                        }
                }
         });