我有数百个选择html标记的列表,这些列表要求用户根据左列的值来选择选项。我使用键来区分每个标记,但是如何在jQuery中编写单击了哪个标记并将选项附加到单击的select html标记呢?现在$('。option')返回所有select html标记,这不是我想要的]
我的HTML刀片
@foreach ($values as $k=>$value)
{!! Form::label('Value',$value['name'],['class' => 'control-label']) !!}
{!! Form::select('option_'.$k, [], '', ['id'=>'option_'.$k,
'class'=>'option form-control selectpicker','required',
'data-live-search'=>'true','data-level'=>$value['level']]) !!}
@endforeach
我的脚本部分
$(document).ready(function(){
$('body').on('click', '.option', function(event){
event.preventDefault();
var level=$('.option').data('level');
var options;
if(level===1){
options=JSON.stringify('{{ json_encode($option_one) }}');
} else if(level===2){
options=JSON.stringify('{{ json_encode($option_two) }}');
} else if(level===3){
options=JSON.stringify('{{ json_encode($option_three) }}');
}
$('#option_'+level).append(options);
});
});
您可以通过以下方法解决此问题:
var level=$('.option').data('level');
使用此:
var level = $(this).data('level');
因此,您不必指向所有.option
元素,而只是指向当前单击的.option
元素。