序列化表单在bootstrap模式中不起作用

时间:2015-10-12 12:27:52

标签: javascript jquery ajax

我正在尝试使用jquery / ajax来序列化对话框中的表单,但是表单提交正常而不是通过ajax,这是我的javascript:

$('.modal-content').on('submit', "send_pm", function() {
    $.ajax({
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        headers: {
            'X-Requested-With': 'XMLHttpRequest'
        },
        dataType: 'json',
        success: function(data) {
            if (!data.success) {
                $.notify(data.error, "error");
            } else {
                $('#globalModal').dialog("close");
                $.notify(data.success, "success");
            }
        }
    });
    return false;
});

我的对话框

<div class="modal" id="globalModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
             </div>
        </div>
    </div>
 </div>

我在jquery上返回false,所以它应该正常提交,这是我的表单:

<form action="<?php echo config::get('URL'); ?>messages/send_message" method="POST" id="send_pm">
    <div class="panel-default m-t-20">
        <div class="panel-body p-t-30">
            <div class="form-group" data-intro="<?php echo Tutorials::tutorial('message_compose_sender_to'); ?>" data-step="2">
                <input type="text" class="form-control" value="<?php echo system::escape($this->to); ?>" id="recipient" name="to">
            </div>
            <div class="form-group" data-intro="<?php echo Tutorials::tutorial('message_compose_subject'); ?> " data-step="3">
                <input type="text" class="form-control" name="subject" value="<?php echo system::escape($this->subject); ?>" placeholder="Subject">
            </div>
            <div class="form-group" data-intro="<?php echo Tutorials::tutorial('message_compose_message_body'); ?> " data-step="4" data-position="top">
                <textarea id="compose-textarea" name="message" class="form-control" style="height: 300px">
                    <?php echo System::wysiwyg($this->
                            message); ?>
                </textarea>
            </div>
            <div class="pull-right" data-intro="<?php echo Tutorials::tutorial('message_compose_discard_message'); ?>" data-step="1">
                <button type="submit" class="btn btn-purple waves-effect waves-light"><span><?php echo System::translate("Send"); ?>
                            </span><i class="icon-mail-send"></i></button>
            </div>
        </div>
    </div>
</form>

1 个答案:

答案 0 :(得分:0)

从我看来,.modal-content是 div &amp; send_pm应该是不在.modal-content div中的表单名称。

所以请使用 $(document).on 代替 $('。modal-content')。on 。 您可以使用合适的选择器代替文档。

另外,正如其他人在评论中指出的那样使用#send_pm