php-在jquery中循环循环laravel数组集合
作者:互联网
我想使用jquery将选择标签附加到我的代码中,并且它的选项需要laravel数组集合中的值
控制者
$array['skills'] = DB::table('skills')->get();
脚本
<script type="text/javascript">
var skill_count=$('#skill_count').val();
var array = {!!$array['skills']!!};
$("#add-skill").click(function(){
skill_count++;
var skill='<div class="row skill"><div class="col-sm-4"><label for="name">Skill:</label><select name="skill['+skill_count+'][skill_name]" class="form-control">$.each( array, function( i, val ) {<option value="+ val.id +"> val.skill_name </option>})</select></div><div class="col-sm-4"><label for="name">Experience:</label><select name="skill['+skill_count+'][pivot][experience_year]" class="form-control">for(i=1;i<=30;i++){<option value="+ i +">i</option>}</select><select name="skill['+skill_count+'][pivot][experience_month]" class="form-control">for(i=1;i<=12;i++){<option value="+i+">i</option>}</select></div><div class="col-md-12"><a class="skill-remove pull-right">- Remove Skill</a></div></div>'
$(".show-skill").before(skill);
});
</script>
但是在浏览器中,val.id和val.skill_name没有获取值.
console.log(val.skill_name);给我价值
解决方法:
希望这会有所帮助
var array = [{
id: 2,
skill_name: 'abc'
}, {
id: 3,
skill_name: 'xyz'
}];
var options;
$.each( array, function( key, value ) {
console.log(value.skill_name);
options = options + '<option value="'+value.id+'">'+value.skill_name+'</option>';
});
var skillhtml = '<div><label for="name">Skill:</label><select name="skillname">'+options+'</select></div>';
$("#container").html(skillhtml);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="container">
</div>
jsfiddle:https://jsfiddle.net/j5nkLabw/
标签:laravel-5-4,php,jquery 来源: https://codeday.me/bug/20191111/2019858.html