Javascript 通过Laravel中的js onclick事件提交数据
如何通过jsonclick事件提交表单?我使用的是Laravel4.1 我试过这个密码Javascript 通过Laravel中的js onclick事件提交数据,javascript,php,laravel,laravel-4,Javascript,Php,Laravel,Laravel 4,如何通过jsonclick事件提交表单?我使用的是Laravel4.1 我试过这个密码 @foreach ($items as $item) <li> {{ Form::checkbox('items', $item->id , $item->done, ['onClick'=> 'this.form.submit()']) }} {{ $item->name }} </li> @endforeach
@foreach ($items as $item)
<li>
{{ Form::checkbox('items', $item->id , $item->done, ['onClick'=> 'this.form.submit()']) }}
{{ $item->name }}
</li>
@endforeach
顺便说一下,在chrome控制台中,我得到了以下错误:
未捕获的TypeError:无法读取null的属性“submit”
有人能帮我吗?据我所知,视图中不应该有多个标记。我建议您使用ajax来触发提交事件。但是,您甚至没有一个表单打开标记,只有一个复选框 我可能需要更多信息,但据我所知,单击复选框时,您需要向控制器提交数据 我会这样做:
// Your view
<!-- CSRF Token -->
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<!-- ./ csrf token -->
@foreach ($items as $item)
<input type="checkbox" name="{{$item->id}}" id="{{$item->id}}" value="{{$item->done}}" class="checkbox_click">
@endforeach
<script type="text/javascript">
$(document).ready(function() {
$('.checkbox_click').click(function(){
var currentValue = $(this).attr("value");
$.ajax({
url: 'your/route',
method: 'post',
data: {id: currentValue, _token: $('input[name="_token"]').val()},
success: function(data){
alert(data);
},
error: function(){},
});
});
});
</script>
// Your controller
public function postIndex()
{
if (Request::ajax)
{
$id = Input::get('id');
return Response::json($id);
}
}
// Your routes.php
Route::post('your/route', YourController@postIndex);
//您的视图
@foreach($items作为$item)
@endforeach
$(文档).ready(函数(){
$('.checkbox_click')。单击(函数(){
var currentValue=$(此).attr(“值”);
$.ajax({
url:'您的/路线',
方法:“post”,
数据:{id:currentValue,_-token:$('input[name=“_-token”]”)val(),
成功:功能(数据){
警报(数据);
},
错误:函数(){},
});
});
});
//你的控制器
公共函数postIndex()
{
if(请求::ajax)
{
$id=Input::get('id');
返回响应::json($id);
}
}
//您的routes.php
路线::post('your/Route',YourController@postIndex);
这是否在表单中?
// Your view
<!-- CSRF Token -->
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<!-- ./ csrf token -->
@foreach ($items as $item)
<input type="checkbox" name="{{$item->id}}" id="{{$item->id}}" value="{{$item->done}}" class="checkbox_click">
@endforeach
<script type="text/javascript">
$(document).ready(function() {
$('.checkbox_click').click(function(){
var currentValue = $(this).attr("value");
$.ajax({
url: 'your/route',
method: 'post',
data: {id: currentValue, _token: $('input[name="_token"]').val()},
success: function(data){
alert(data);
},
error: function(){},
});
});
});
</script>
// Your controller
public function postIndex()
{
if (Request::ajax)
{
$id = Input::get('id');
return Response::json($id);
}
}
// Your routes.php
Route::post('your/route', YourController@postIndex);