Laravel Checkbox在实时搜索中始终返回true

时间:2017-10-10 08:06:16

标签: javascript forms laravel checkbox livesearch

我的复选框有问题:执行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']);
    }
}

1 个答案:

答案 0 :(得分:2)

您的复选框的1始终为// boolean var deleted = $("#search-deleted").prop('checked'); ;你真正想知道的是它是否被检查。

{{1}}