Laravel - 使用 onclick 从复选框提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23331597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - using onclick to submit form from checkbox
提问by kevo
I have a simple task list project under Laravel.
我在 Laravel 下有一个简单的任务列表项目。
When I click a checkbox it does not show a checked condition. (The second item is in the true condition in the database and thus shows checked. I cannot uncheck this item) I have searched for an answer to why on the net but cannot find a solution or reason.
当我单击复选框时,它不会显示选中的条件。(第二项在数据库中处于真实状态,因此显示为已选中。我无法取消选中此项)我在网上搜索了原因的答案,但找不到解决方案或原因。
Code:
代码:
home.blade.php (in views folder) -
home.blade.php(在视图文件夹中)-
@extends('layouts.main')
@section('content')
<h1>Tasks</h1>
<ul>
@foreach ($items as $item)
<li>
{{ Form::open() }}
<input type="checkbox" onClick="this.form.submit()" {{ $item->done ? 'checked' : '' }}>
<input type="hidden" name="id" value="{{ $item->id }}">
{{ $item->name }}
{{ Form::close() }}
</li>
@endforeach
</ul>
@stop
HomeController.php (inControllers folder) -
HomeController.php (inControllers 文件夹) -
<?php
class HomeController extends BaseController {
public function getIndex() {
$items = Auth::user()->items;
return View::make('home', array(
'items' => $items
));
}
public function postIndex() {
$id = Input::get('id');
$user_id = Auth::user()->id;
$item = Item::findOrFail($id);
if($item->owner_id == $userId) {
$item->mark();
}
return Redirect::route('home');
}
}
Item.php (in models folder) -
Item.php(在模型文件夹中)-
<?php
class Item extends Eloquent {
public function mark() {
$this->$done = $this->done ? false : true;
$this->save();
}
}
routes.php -
路线.php -
<?php
Route::get('/', array('as' => 'home', 'uses' => 'HomeController@getIndex'))->before('auth');
Route::post('/', array('uses' => 'HomeController@postIndex'))->before('csrf');
Route::get('/login', array('as' => 'login', 'uses' => 'AuthController@getLogin'))->before('guest');
Route::post('login', array('uses' => 'AuthController@postLogin'))->before('csrf');
回答by zwacky
in your code, you never update the model's donevalue. i assume, you want to change it with the post method. so you'd need to take the value from the checkbox name (e.g. Input::get('box-ID')
)
在您的代码中,您永远不会更新模型的完成值。我假设,您想使用 post 方法更改它。所以你需要从复选框名称中获取值(例如Input::get('box-ID')
)
you could also create a checkbox using the form class:
您还可以使用表单类创建一个复选框:
// public function checkbox($name, $value = 1, $checked = null, $options = array())
{{ Form::checkbox('name', 'value', true, ['onClick' => 'alert(123)']) }}
reference: Formbuilder -> checkbox
回答by Aman Ullah
You should modify your form like this. It works me I hope will work for you also.
你应该像这样修改你的表单。它对我有用,我希望也对你有用。
{{ Form::open(['route' => ['items.update', $items->id], 'class' => 'form-inline', 'method' => 'put']) }}
{{ Form::open(['route' => ['items.update', $items->id], 'class' => 'form-inline', 'method' => 'put']) }}
Thanks
谢谢