【发布时间】:2020-02-11 07:02:15
【问题描述】:
在我的网站上,我希望有多种表格来对不同的帖子进行投票。我已经为此实现了 AJAX,它工作正常,但总是只使用第一种形式。我知道我应该在我的代码中改变一些东西来做到这一点(一些带有 id 的东西),但老实说,我迷路了。请多多指教。
$(document).ready(function() {
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$("#negative_button").click(function() {
$.ajax({
/* the route pointing to the post function */
url: '/negative',
type: 'POST',
/* send the csrf-token and the input to the controller */
data: {
_token: CSRF_TOKEN,
negative_vote: $("#negative_vote").val(),
post_id: $("#post_id").val()
},
dataType: 'JSON',
/* remind that 'data' is the response of the AjaxController */
success: function(data) {
$(".writeinfo").append(data.alert);
}
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="pull-right inline">
<meta name="csrf-token" content="{{ csrf_token() }}" />
<input type="hidden" id="negative_vote" name="negative_vote" value="{{Auth::user()->id}}">
<input type="hidden" id="post_id" name="post_id" value="{{$post->id}}">
<input type="button" id="negative_button" class="btn btn-danger" value="Downvote">
</div>
<div class="writeinfo"></div>
【问题讨论】:
标签: javascript php ajax laravel forms