weixin_33695082 2013-09-02 15:12 采纳率: 0%
浏览 47

为什么要提交Ajax表单?

我知道这个问题已经问过无数次了,我看过几个例子,但我还是不知道为什么要提交这个表格。Ajax似乎没有被调用,所以我认为它就是简单的divid问题。这30分钟我一直觉得很沮丧......

JS:

$('#genform').submit(function (e) {
    alert('hi');
    e.preventDefault();
    $.ajax({
        url: "month.php",
        type: "post",
        data: $('#form').serialize(),
        success: function (msg) {
            $("#info").html(msg);
        }
    });
});

HTML:

<!-- trigger button -->
<div class="col-md-4">
<a href="#" id="toggle" class="btn btn-default dropdown-toggle btn-sm"> Bulk PDF Export <span class="caret"></span></a> 
</div>
<!--- popup form div -->
<div id="gendiv" style="display:none;">
    <form id="genform">
        <div class="form-input">
            <select name="month">
                <option value="2013-09-01">September 2013</option>
                <option value="2013-08-01">August 2013</option>
                <option value="2013-07-01">July 2013</option>
            </select>
        </div>
        <div class="form-input"><i class="icon-ellipsis-horizontal"></i> PGY-1  <span class="pull-right"><input type="checkbox" id="pgy1" checked name="pgy[1]"></span> 
        </div>
        <div class="form-input"><i class="icon-ellipsis-horizontal"></i> PGY-2  <span class="pull-right"><input type="checkbox" id="pgy2" checked name="pgy[2]"></span> 
        </div>
        <div class="form-input"><i class="icon-ellipsis-horizontal"></i> PGY-3  <span class="pull-right"><input type="checkbox" id="pgy3" checked name="pgy[3]"></span> 
        </div>
        <div class="form-input" style="text-align:center">
            <button type="submit" class="btn btn-primary btn-xs">Generate</button>
        </div>
        <div id="info"></div>
    </form>
</div>

Fiddle: http://jsfiddle.net/KQ2nM/2/

  • 写回答

2条回答 默认 最新

  • 普通网友 2013-09-02 15:21
    关注

    You are missing some closing tags:

    <div class="form-input">
        <i class="icon-ellipsis-horizontal"></i> PGY-1 <span class="pull-right">
            <input type="checkbox" id="pgy1" checked name="pgy[1]">   </input>    <--- here
        </span>
    </div>
    

    And the form method (otherwise it spits up an error):

    <form id="genform" method="POST">
    

    Now django complains about the CSRF token, but that's your stuff ;)
    Here's the new Fiddle.

    EDIT: It seems like I got it wrong since now it submits without calling your custom handler and Joe fixed it. But you still need to close those inputs :)

    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题