weixin_33747129 2017-02-20 22:23 采纳率: 0%
浏览 14

提交AJAX发布变更

I'm trying to figure out why this isn't working, I don't want to have a submit button to click, It does work if I have one though, instead I use onchange="this.form.submit()" and that posts the form as it normally would, not AJAX background style, I didn't code the ajax part, I found it and made it work for my situation, but as far as I know $('#ajaxform').submit(function (), submit is submit? Why isn't onchange="this.form.submit()" and <input type="submit" /> the same type of submit? What am I missing?

    <form method="post" action="~/getAJAX.cshtml" id="ajaxform" name="form">
        @* -------- Div to hold form elements -------- *@
        <div class="reportDateDiv">

            @* -------- Text --------- *@
            <a class="blackColor fSize18 RPtxt">Reporting Period</a>

            @* -------- Start day box -------- *@
            <input type="text" name="inputDate" spellcheck="false" class="datepickerS metricDateTextbox capitalFirst"
                  onchange="this.form.submit()" value="@inputDate" autocomplete="off" placeholder="@placeholderStartDate.ToString("MMM d, yyyy")" readonly="readonly" />

            @* -------- Text --------- *@
            <a class="blackColor fSize16 RPtxt RPtxtTo">to</a>

            @* -------- End day box --------- *@
            <input type="text" name="endDate" spellcheck="false" class="datepickerE metricDateTextbox capitalFirst"
                  onchange="this.form.submit()" value="@endDate" autocomplete="off" placeholder="@noEndDate.ToString("MMM d, yyyy")" readonly="readonly" />

        </div>
    </form>

    <script type="text/javascript">
        $('#ajaxform').submit(function () { // catch the form's submit event
            $.ajax({ // create an AJAX call...
                data: $(this).serialize(), // get the form data
                type: $(this).attr('method'), // GET or POST
                url: $(this).attr('action'), // the file to call
                success: function (response) { // on success..
                    $('#here').html(response); // update the DIV
                }
            });
            return false; // cancel original event to prevent form submitting
        });
    </script>
  • 写回答

1条回答 默认 最新

  • weixin_33737134 2017-02-20 22:42
    关注

    Use this in your form:

    <input ... onchange="mySubmit(this.form)" ... >
    

    Change the script to this:

    function mySubmit(theForm) {
        $.ajax({ // create an AJAX call...
            data: $(theForm).serialize(), // get the form data
            type: $(theForm).attr('method'), // GET or POST
            url: $(theForm).attr('action'), // the file to call
            success: function (response) { // on success..
                $('#here').html(response); // update the DIV
            }
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口