Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery TypeError:$(…).val(…)为空_Javascript_Jquery_Laravel_Laravel 5_Jquery Select2 - Fatal编程技术网

Javascript jQuery TypeError:$(…).val(…)为空

Javascript jQuery TypeError:$(…).val(…)为空,javascript,jquery,laravel,laravel-5,jquery-select2,Javascript,Jquery,Laravel,Laravel 5,Jquery Select2,我似乎找不到代码中的漏洞。我使用select2作为我的选择列表,我的右侧有一个按钮,单击该按钮时,将执行以下脚本: <script type="text/javascript"> function clearBiller() { $('#bill_to').val('').trigger('change'); } </script> 这个问题在第二个函数中的某个地方,我只是不知道如何确定它。目前,这是表单中该部分的html: <select id="bil

我似乎找不到代码中的漏洞。我使用select2作为我的选择列表,我的右侧有一个按钮,单击该按钮时,将执行以下脚本:

<script type="text/javascript">
function clearBiller() {
    $('#bill_to').val('').trigger('change');
}
</script>
这个问题在第二个函数中的某个地方,我只是不知道如何确定它。目前,这是表单中该部分的html:

<select id="bill_to" name="bill_to" class="js-example-basic-single form-control">
                    @if($shipment->bill_to)
                        <option value="{{$shipment->bill_to}}" selected>3</option>
                    @endif

                    @foreach($cCustomers as $customer)
                        <option value="{{$customer->id}}">{{$customer->customer_name}}</option>
                    @endforeach
                </select>
                <img src="/images/clear.png" height="15px" width="15px" style="margin-left: 5px;" onclick="clearBiller()">
                <div id="existing_biller_details" class="hidden" name="existing_biller_details" style="margin-top:10px;">
                </div>
上面的警告只是用来指导我哪些地方不起作用。此时,我加载了我已将id为“existing\u biller\u details”的div设置为已隐藏的页面,但如果在select2中选择了一个值,则“hidden”类将被删除


我目前的问题是,使用上面的代码,只有“else”可以完全工作。如果我遍历初始的“If”语句,它将只执行
$('#biller').removeClass('hidden')
,然后它就不能再继续了。我在控制台中没有看到任何错误,但没有出现警报,并且命令
$(“#existing_biller_details”).addClass('hidden')
也不起作用。

您应该直接在
上触发更改事件

$('#bill_to').val('');
$('#bill_to').trigger('change');
但是,您应该选中Select2文档,因为当Select2初始化时,您的主选择被隐藏,并且Select2有自己的更改事件集等等。

这对您有帮助吗

var $eventSelect = $("#bill_to");
    $eventSelect.select2();
    $eventSelect.on("select2:select", function (e) {
          if ($eventSelect.select2().val() == '')
          {
            $("#biller").show();
          }
          else
          {
            $("#biller").hide();
          }
    });

选中并选择2个文档。它有自己的事件。好吧,这适用于select2,我上面指定的错误是引用第二个函数。实际上是$jQuery,因为它不应该返回null…好的,检查我的答案,但它不会有多大帮助。仍然需要检查Select2 API。请参阅我的更新(几分钟后),您的更新工作得最好,但我写这篇文章的方式肯定仍然存在缺陷。
$('#bill_to').val('');
$('#bill_to').trigger('change');
var $eventSelect = $("#bill_to");
    $eventSelect.select2();
    $eventSelect.on("select2:select", function (e) {
          if ($eventSelect.select2().val() == '')
          {
            $("#biller").show();
          }
          else
          {
            $("#biller").hide();
          }
    });