【问题标题】:jquery submit ajaxSubmit() not workingjquery提交ajaxSubmit()不起作用
【发布时间】:2026-02-12 16:55:02
【问题描述】:

我在使用 cakePHP 设置文件上传元素以在表单中使用时遇到问题,并且 ajaxSubmit() 函数不发送 ajax 请求。我的代码如下,任何想法表示赞赏

在另一个元素中,我也非常成功地使用 .ajaxForm() 方法上传文件。

  <script>
        $(document).ready(function() {

            var bar = $('.bar');
            var percent = $('.percent');
            var status = $('#status');

            $('#frmFileForm1').ajaxForm({
                url: 'http://up.dev/admin/pages/file',
                beforeSend: function() {
                    status.empty();
                    var percentVal = '0%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                uploadProgress: function(event, position, total, percentComplete) {
                    var percentVal = percentComplete + '%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                success: function() {
                    var percentVal = '100%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                complete: function(xhr) {
                    status.html(xhr.responseText);
                    reloadfilemanager();
                }
            });

                var options = {
                    url: 'http://up.dev/admin/pages/file',
                    iframe: true,
                    type: 'post',
                    target: '#status',
                    data: 'submitBtn'
                    }


             $('button').click(function() {
                $('#frmFileForm1').ajaxSubmit(options);
               alert("AA");
             });


        });

    </script>
<div>
        <?php

//<!-- form for uploading file -->

        echo $this->Form->button('Upload File', array( 'name' => 'submitBtn', 'class' => 'btn_submit', 'style' => 'margin-top: 5px; margin-bottom: 5px;', 'type' => 'submit'));
        ?>
    </div>

【问题讨论】:

    标签: php jquery ajax cakephp jquery-forms-plugin


    【解决方案1】:

    我认为你应该改变你的选择器:

    $('button').click(function() {
    

    改成这样:

    $('.btn_submit').click(function() {
    

    因为您的按钮类型是 type="submit",而您的选择器的目标是 type="button",这在我看来是不可用的。


    更新:

    而不是click 按钮尝试使用submit 事件:

    var options = {
               url: 'http://up.dev/admin/pages/file',
               iframe: true,
               type: 'post',
               target: '#status',
               data: 'submitBtn'
             }; //<----add ; here not that much issue with this though
    
    
    $('#frmFileForm1').submit(function(e) {
        e.preventDefault();
        $(this).ajaxSubmit(options);
        alert("AA");
    });
    

    【讨论】:

    • 我尝试使用 $('.btn_submit').click(function() { 仍然没有触发 ajaxSubmit() 以我的理解问题不是按钮触发,因为警报有效.. 之后。 ajaxSubmit();
    • @Prasad 查看更新。
    • Hai Jai 感谢更新,不幸的是没有运气.. 可能是因为表格在另一个表格中..
    • @prasad 是的,应该是这样。