为输入分配值,需要两个帖子

时间:2017-03-02 10:06:14

标签: javascript jquery html ajax

<li>点击我将值设置为隐藏的输入,如此

$(".total").click(function () {
    $("input[name=hid4]").val("4");
});

点击我的<li>,它还会将带有ajax的表单发布到获取所有数据并返回的页面。

但是,在该页面上,我请求我刚设置的值var getVal = request.form["hid4"];,然后进行测试我就像这样<a>@getVal</a>写出来,它在更新之前需要2个帖子,为什么就是它?如何在首次发布后更新?

Ajax代码:

function mySubmit(theForm) {
    theForm = $(theForm).closest("form");
    $.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
        }
    });
}

以表格列出

<div class="holdLiftMenu">
    <ul class="holdLiftMenuUL">
        <li class="holdLiftMenuLI">
            <a onclick="mySubmit(this)" class="holdLiftMenuA total current">Total
                <input type="hidden" name="hid4" id="hid4" value="" />
            </a>
        </li>
    </ul>
</div>

1 个答案:

答案 0 :(得分:3)

您可以从此解决方案中进行选择

Sol 1- remove onclick =“mySubmit(this)”并更改

$(".total").click(function () {
   $("input[name=hid4]").val("4");
   mySubmit($(this))
  });

Sol 2- remove $(“。total”)。click(function()并添加$(“input [name = hid4]”)。val(“4”);在函数mySubmit(theForm)

function mySubmit(theForm) {
 $("input[name=hid4]").val("4");
theForm = $(theForm).closest("form");
$.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
    }
   });
}