Laravel 复选框 returns 在实时搜索中始终为真
Laravel Checkbox returns always true in live search
我的复选框有问题:在进行 LIVEsearch 时,复选框的值始终设置为 1/true。当我在正常搜索(没有 js 文件)中做同样的事情时,我没有这个问题。
首先是我的复选框的代码:
{{ Form::checkbox('deleted', '1' , null , array('id' => 'search-deleted'))}}
比我的代码javascript(我猜问题是获取复选框的值)
<script>
$(document).ready(function(){
$(".search").keyup(function(){
var staffid= $("#search-staffid").val();
var firstName= $("#search-first-name").val();
var lastName= $("#search-last-name").val();
var birthday= $("#search-birthday").val();
var children= $("#search-children").val();
var deleted= $("#search-deleted").val();
if(staffid == "" && firstName == "" && lastName == "" && birthday == "" && children == "" ) {
$( "#liveResult" ).html("<b>Suchergebnisse werden hier angezeigt</b>");
}else {
$.get( "{{ url('demos/livesearch') }}" +
'?staffid=' + staffid +
'&first_name=' + firstName +
'&last_name=' + lastName +
'&birthday=' + birthday +
'&children=' + children +
'&deleted=' + deleted
, function( data ) {
$( "#liveResult" ).html( data );
});
}
});
});
</script>
如您所见,我还有其他一些字段,它们可以正常工作。最后是我的控制器代码:
public function liveSearch(Request $request)
{
$data = [
'id' => $request->staffid,
'first_name' => $request->first_name,
'last_name' => $request->last_name,
// converts date format only if birthday != null
'birthday' => ($request->birthday ? date('Y-m-d', strtotime($request->birthday)) : null),
'children' => $request->children,
'deleted' => $request->deleted
];
if (is_null($data['id']) && is_null($data['first_name']) && is_null($data['last_name']) && is_null($data['birthday']) && is_null($data['children']))
{
return view('staff.livesearch');
}
else
{
dd ($data['deleted']);
}
}
你的复选框的value
总是1
;你真正想知道的是它是否被选中。
// boolean
var deleted = $("#search-deleted").prop('checked');
我的复选框有问题:在进行 LIVEsearch 时,复选框的值始终设置为 1/true。当我在正常搜索(没有 js 文件)中做同样的事情时,我没有这个问题。 首先是我的复选框的代码:
{{ Form::checkbox('deleted', '1' , null , array('id' => 'search-deleted'))}}
比我的代码javascript(我猜问题是获取复选框的值)
<script>
$(document).ready(function(){
$(".search").keyup(function(){
var staffid= $("#search-staffid").val();
var firstName= $("#search-first-name").val();
var lastName= $("#search-last-name").val();
var birthday= $("#search-birthday").val();
var children= $("#search-children").val();
var deleted= $("#search-deleted").val();
if(staffid == "" && firstName == "" && lastName == "" && birthday == "" && children == "" ) {
$( "#liveResult" ).html("<b>Suchergebnisse werden hier angezeigt</b>");
}else {
$.get( "{{ url('demos/livesearch') }}" +
'?staffid=' + staffid +
'&first_name=' + firstName +
'&last_name=' + lastName +
'&birthday=' + birthday +
'&children=' + children +
'&deleted=' + deleted
, function( data ) {
$( "#liveResult" ).html( data );
});
}
});
});
</script>
如您所见,我还有其他一些字段,它们可以正常工作。最后是我的控制器代码:
public function liveSearch(Request $request)
{
$data = [
'id' => $request->staffid,
'first_name' => $request->first_name,
'last_name' => $request->last_name,
// converts date format only if birthday != null
'birthday' => ($request->birthday ? date('Y-m-d', strtotime($request->birthday)) : null),
'children' => $request->children,
'deleted' => $request->deleted
];
if (is_null($data['id']) && is_null($data['first_name']) && is_null($data['last_name']) && is_null($data['birthday']) && is_null($data['children']))
{
return view('staff.livesearch');
}
else
{
dd ($data['deleted']);
}
}
你的复选框的value
总是1
;你真正想知道的是它是否被选中。
// boolean
var deleted = $("#search-deleted").prop('checked');