通过 jQuery 发布发布表单 - 没有控制台错误

Issue Posting Form Via jQuery - No Console Errors

本文关键字:控制台 错误 表单 jQuery 布发布 通过      更新时间:2023-09-26

我正在尝试通过jQuery .post()发布表单,但它无法正常工作。我从控制台上一无所获,这非常令人沮丧。向有经验的 JS 用户请求帮助。非常感谢您的帮助!

注意:此表单是使用 CodeIgniter 构建的,并且使用其表单助手和 HTML 常规路由可以完美地发布。JS/jQuery方法是无法正常工作的。

形式:

    <form class="nice custom" id="create_form">
                    <h3>Create Event</h3>
                    <label for="intention"><strong>Intention:</strong></label>
                    <?php $intention_attributes='style="width: 100%; " class="show-on-phones" id="intention_dropdown"'; ?>
                    <?php echo form_dropdown('intention', $intention, 'Select Intention', $intention_attributes ); ?>
                    <div class="custom dropdown show-on-desktops" style="width: 100%; ">
                        <a href="#" class="current">Select Intention</a>
                        <a href="#" class="selector"></a>
                        <ul style="width: 100%; ">
                            <li>Select Intention</li>
                            <li>Option 1</li>
                            <li>Option 2</li>
                            <li>Option 3</li>
                            <li>Option 4</li>
                            <li>Option 5</li>
                        </ul>
                    </div>
                    <label for="action"><strong>Action:</strong></label>
                    <?php echo form_input($action); ?>
                    <label for="date_of_event"><strong>Date:</strong></label>
                    <?php echo form_input($date_of_event); ?>
                    <label for="repeat_event"><strong>Repeat:</strong></label>
                    <?php $repeat_event_attributes='style="width: 100%; " class="show-on-phones" id="repeat_event_dropdown"'; ?>
                    <?php echo form_dropdown('repeat_event', $repeat_event, 'Never', $repeat_event_attributes); ?>
                    <div class="custom dropdown show-on-desktops" style="width: 100%; ">
                        <a href="#" class="current">Never</a>
                        <a href="#" class="selector"></a>
                        <ul style="width: 100%; ">
                            <li>Never</li>
                            <li>Every Day</li>
                            <li>Every Week</li>
                            <li>Every Two Weeks</li>
                            <li>Every Month</li>
                            <li>Every year</li>
                        </ul>
                    </div>
                    <label for="end_repeat_event"><strong>End Repeat:</strong></label>
                    <?php echo form_input($end_repeat_event); ?>
                    <br />
                    <input style="width: 100%; " type="submit" name="submit" class="medium radius blue button" value="Create" id="create_button" />
</form>

JavaScript:

// Submit Create Form
    $('#create_form').live('submit', function() {
        var data = $('#create_form').serialize();
        var url = $(this).attr('action');
        $.post(url, data, function() {
            document.location.reload();
        });
        return false;
    }); // End

表单没有操作值:

<form class="nice custom" id="create_form">

即使您试图获得一个:

var url = $(this).attr('action');

另外,从现在开始避免使用$.live

从 jQuery 1.7 开始,.live() 方法已被弃用。使用 .on() 附加事件处理程序。旧版本的jQuery的用户应该优先使用.delegate()而不是.live()

— http://api.jquery.com/live/

尝试将提交按钮的名称更改为 btnSubmit。 我过去在命名提交按钮提交时遇到过问题。

谢谢

另外,按控制台输出选项卡顶部的黑点按钮(webkit 浏览器)。 这将在页面加载之间保留日志消息。