简体   繁体   English

获取laravel中的选择列表的值

[英]Get the value of select list in laravel

I created a blade with the following dropdown 我用以下下拉菜单创建了一个刀片

  <?php
                 $Years = App\InvoiceHeader::pluck('year')
                                           ->all() ;
               ?>
               {{ Form::select('Year', $Years, null) }}

I want to use the selected year in another PHP tag in the same blade. 我想在同一刀片中的另一个PHP标记中使用选定的年份。 How can I get the selected value 如何获得所选值

PHP only runs on server side, this means that you can't manipulate it after the content of the page are sent to the client (browser). PHP仅在服务器端运行,这意味着在将页面内容发送到客户端(浏览器)后,您将无法对其进行操作。 So you can't get the value of the input and use for another thing with PHP without send the data to the server and reload the page with the new information. 因此,如果不将数据发送到服务器并用新信息重新加载页面,就无法获得输入的值并将其用于PHP。 But it isn't a good thing to do and provides a poor user experience. 但这不是一件好事,并且会带来糟糕的用户体验。

For manipulate the page on the browser without need to reload we have JavaScript. 为了在浏览器上操作页面而无需重新加载,我们提供了JavaScript。

In your case you need to add an onchange event on select: 在您的情况下,您需要在select上添加一个onchange事件:

{{ Form::select('Year', $Years, null, ['onchange' => 'yearChanged(event)']) }}

Then you create the Javascript function: 然后创建Javascript函数:

<script>
    function yearChanged(e) {
        //Do anything you want
    }
</script>

To get the value of the select inside this function you can use e.target.value 要在此函数中获取select的值,可以使用e.target.value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM