dongxinyue2817 2014-12-02 12:20
浏览 32
已采纳

如何将刀片模板数组附加到javascript中

I am developing a laravel application with blade template. Screenshot of the current layout

When I click on the plus button, I get an additional field and when I click the minus button it gets removed properly and working fine.

However, I need to generate the additional section as a select option box, and this select option box has to show dynamic values.

<div class="RegSpLeft" id="affiliation">
  <select>
     @foreach($affiliation_array as $state_data)
      <option>{{$state_data->value}}</option>
     @endforeach
  </select>

</div>

I want to display above array named affiliation_array.

I'm using the following script to generate additional fields:

   $('.affiliation_add a.pl').click(function(e) {

        e.preventDefault();
        $('#affiliation').append('<select><!--here i want to append my array as options --></select>');
        num2++
    });
    $('.affiliation_add a.mi').click(function (e) {
        e.preventDefault();
        if ($('#affiliation select').length > 1) {
            $('#affiliation').children().last().remove();
        }
    }); 

My button div is below,

<div class="RegSpRight affiliation_add"> 
  <a href="#" class="pl"><img src="images/plus.png"></a>
  <a href="#" class="mi"><img src="images/minus.png"></a> 
</div>

Anybody please help me to solve this issue.

  • 写回答

1条回答 默认 最新

  • dongshuofu0039 2014-12-02 13:14
    关注

    This should work:

     $('.affiliation_add a.pl').click(function(e) {
    
            e.preventDefault();
            $('#affiliation').append('<select>@foreach( $affiliation_array as $state_js )<option> {{   $state_js->value }}</option> @endforeach</select>');
            num2++
        });
        $('.affiliation_add a.mi').click(function (e) {
            e.preventDefault();
            if ($('#affiliation select').length > 1) {
                $('#affiliation').children().last().remove();
            }
        });
    

    You can copy and paste that into you project. You should just remember to change the variable name $state_js to any other name you like, or you could still leave it as that if you dont mind.You can also decide to add the option name and value to the option i put there. For example:

    <option value={{ $state->value }}> {{ $state_js->value }}</option>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能