Laravel Collective onclick in SELECT



如何在"select"中添加laravel collective onclick?我的以下代码不起作用:

{{Form::select('material_selector', [
'10' => 'Cotton', 
'20' => 'Wet Look',
'30' => 'Crocodile',
], null, ['placeholder' => 'Select Material'],['class'=>'form-control','onclick'=> 'showSelectedValue()' ])
}}

您可能应该使用jQuery。然后,您可以按照以下对您选择的元素进行寻址

$(document).ready(function() {
$('.form-control[name="material_selector"]').on('change', showSelectedValue);

function showSelectedValue(event) {
var target = $(event.target);
console.log(target.val() + " = " + target.find('option:selected').text());
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="material_selector" class="form-control">
<option value="10">Cotton</option>
<option value="20">Wet Look</option>
<option value="30">Crocodile</option>
</select>

如果你真的需要使用onClick属性,你可以试试这个

{{Form::select(
'material_selector', // name attribute of the select
['10' => 'Cotton', '20' => 'Wet Look', '30' => 'Crocodile'], // option values
null, // selected value, for example '20'
['placeholder' => 'Select Material', 'class' => 'form-control'], // attributes for <select>
['onClick' => 'showSelectedValue()'] // attributes for <option>
)}}

最新更新