您好我正在创建多个复选框和一个删除按钮。这个删除按钮用于删除多个复选框值。我试试这个,但当我使用火虫控制台时,给出错误的时间是www.xyz.com/undefined 我的代码在下面
message.blade.php
<button type="submit" class="delete-msg btn btn-md btn-round btn-green pull-right hug-me-tight worldoo-btn-xs">Delete</button>
</p>
<div class="clearfix"></div>
@foreach ($messages as $message)
<li class="{{ $message == $messages[0] ? "active" : ""}}">
<a class="message-sender-tab" data-sender-id="{{$message->sender_id}}" href="#v-home" data-toggle="tab">
<input type="checkbox" name="check_box" data-id="check_box" value="{{$message->sender_id}}" />
<span class="photo"> <img alt="" class="img-circle" src="{{$message->sender->avatar->image->url()}}"></span>
<span class="subject"> <span class="from">{{$message->sender->username}}</span> </span>
<span class="message">{{$message->message}}</span>
<span class="time">{{$message->created_at->diffForHumans()}}</span>
</a>
</li>
@endforeach
messenger.js
$(document).ready(function(){
$('.delete-msg').click(function(){
$.post("/api/v1/messages/delete/"+ $(this).data('check_box'), function(data){
alert("Message delete!");
}); });});
routes.php文件
Route::post("/messages/delete/{sender_id}",array("uses" => "MessagesController@deleteMessages"),function($sender_id){
return $sender_id; });
控制器
public function deleteMessages($sender_id)
{
$user = Auth::user()->user();
$current_user_id = $user->id;
$messages = Message::whereRaw('sender_id = ? and recipient_id = ?', array($sender_id, $current_user_id))->delete();
if (Request::wantsJson())
{
return Response::json($messages->toJson());
}
}
答案 0 :(得分:0)
CSS类为delete-msg
的元素是一个按钮标记,并且没有设置data
个属性。因此,当你打电话
$(this).data('check_box')
它不存在。
您应该在按钮点击时选择所有复选框元素。
答案 1 :(得分:0)
实际上我没看到你从复选框中获取数据的位置。为什么不接受它们,比方说如下:
// if u have a form with id
$(".delete-msg").click(function() {
$("input", "#formID").attr("checkboxName");
});
// if u dont have a form, then you can get an array of all input name
$(".delete-msg").click(function() {
$("input").attr("checkboxName");
});
因此您可以将其分配给变量。实施例
Var checkName = $("input").attr("checkboxName");