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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?