duanqiang3925 2015-12-21 18:10
浏览 52
已采纳

两个提交一个ajax一个常规形式

I have this form that i am trying to submit with ajax as well as the regular submit the regular submit is for creating a pdf and the ajax submit is for showing an html example for showing the preview which both work just fine if i use them both individually or use the create pdf before the preview submit but if sumbit for pdf after the preview submit it's not functional, nothing happens like regular submit button is disabled. My code is attached as folows, Do note both work fine just the regular post doesn't work if i use ajax submit first.

<script type="text/javascript">
    $('#submitpdf').submit(function() {
        return true;
    });

    $('#submitpreview').click(function() {
        $('#form').submit(function(event) { // catch the form's submit event
            $.ajax({ // create an AJAX call...

                data: $(this).serialize(), // get the form data
                type: $(this).attr('GET'), // GET or POST
                url: 'test.php', // the file to call
                success: function(response) { // on success..
                    $('#created').html(response); // update the DIV

                }
            });
            return false;
        });
    });
</script>

and here is the HTMl of the form tag and the submit buttons:

<form action="dopdf.php" name="formular" id="form" method="GET" enctype="multipart/form-data">
<input type="submit" id="submitpdf" value="Create PDF" name="print" class="btn btn-primary submit-button" onclick="javascript:document.getElementById('act').value='0'" style="margin-left: 0;" />
<input id="submitpreview" type="submit" value="Preview!" name="preview" class="btn btn-primary submit-button" onclick="javascript:document.getElementById('act').value='0'" style="margin-left: 0;" />
      </form>

One other thing to note is that with ajax i want to submit to "test.php" and with regular submit i want to submit to "dopdf.php".

  • 写回答

2条回答 默认 最新

  • dth2331 2015-12-21 18:21
    关注

    The code for submitPreview button click does not look correct to me ( on the click handler, you are basically registering the submit event code!). Try changing to this clean version. Also, make sure to wrap your event handler code inside the document.ready event to avoid other issues.

    Also you need to read the method attribute of the form, not GET attribute.

    $(function(){
    
       $('#submitpreview').click(function(e) {
    
          e.preventDefault(); // prevent the default form submit behaviour
          var _this=$(this).closest("form");
    
          $.ajax({ // create an AJAX call...
    
            data:_this.serialize(), // get the form data
            type: _this.attr('method'), // GET or POST
            url: 'test.php', // the file to call
            success: function(response) { // on success..
               $('#created').html(response); // update the DIV    
            }
    
          });
      });
    
    });
    

    Since your form action value is set to dopdf.php, when user clicks that button, It will be submitted to dopdf.php. You do not need any additional jQuery click handler for that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)