【问题标题】:Submit form input value on page load with ajax使用ajax在页面加载时提交表单输入值
【发布时间】:2018-06-20 15:00:02
【问题描述】:

我有表单和一个输入,我想将它的值发送到我的控制器。我想在页面加载或使用 ajax 和 jquery 刷新当前页面时发送它的值,因为当我在正文中编写提交函数时我的页面重新加载标签 onload 事件。 这是我的代码:

<form method="post" id="counterForm" name="counterForm">
    @csrf
    <input type="number" name="count" id="count" value="{{ $visit->counts }}">
</form>

脚本代码:

$(window).load(function(){
                    var n=document.getElementById("count").value;
                    $.ajax({
                        type:'POST',
                        url:'/count',
                        data:n,
                    });
                });

我的控制器文件:

public function count(Request $request)
    {
        $value=($request->count)+1;
        DB::table('visitorcounter')->update(['counts'=>$value]);
    }

那是我的代码,但它不起作用...谢谢。

【问题讨论】:

  • 怎么了?具体是什么不起作用

标签: javascript jquery ajax


【解决方案1】:

你可以使用 .submit() :

将事件处理程序绑定到“提交”JavaScript 事件或触发器 元素上的那个事件。

你的代码会是这样的:

$(window).load(function() {

     $('#counterForm').submit(function(){return true;});
});

看看这个例子:

$(window).load(function(){
    // this is the id of the form
    var url = "results.php"; // the script where you handle the form input.
    $.ajax({
           type: "POST",
           url: url,
           data:$( "#myformR" ).serialize(),
           dataType: "html", //expect html to be returned                
           success: function (response) {
                $("#prores").html(response);
            }
         });
});

【讨论】:

  • 如果您有帮助,请在此处发表评论。
【解决方案2】:

我找到了...我们可以非常简单地创建一个访客计数器,方法是使用一个包含一个整数列和一个 DB::table('test')->increment('count'); 的表。在 AppServiceProvider 启动函数中。

【讨论】:

    猜你喜欢
    • 2018-04-10
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    相关资源
    最近更新 更多