duanlv2788 2013-01-09 10:09
浏览 49
已采纳

从Smarty PHP循环(JQuery)中的select中获取选项值

So I need to append and un-append divs which contain the values of whichever option was selected, problem is that the method of displaying the select values in the first place is via. a for-each loop using Smarty PHP templates, and I am unable to find any documentation on how it can be achieved using this.

What I've tried: as you will see below I've tried creating a JavaScript variable inside the template file, but it only ever returns the value of the first option selected. I cannot figure out why I am only ever getting the first value taken from the loop.

For example, if I have two items in a select 'A' and 'B', when selecting the value option, I will always just receive 'A' back as the value.

Smarty Template Code:

<select class="my_select" name="vlans_{$NetworkDTO->id}[]">
    {foreach from=$arrNetworkDTO item=NetworkDTO name=arr}
    <option class="addOvmCardList" id="addNameDisk{$strIdTpl}" value="{$NetworkDTO->name|escape}">{$NetworkDTO->name}</option>
    {/foreach}
</select>

JQuery Code:

$('a.addListOVM').click(function(){

    $(".my_select").change(function() {

        $('.addCardOVM').append(
              '<div class="divform"><label>' + $(this).val() + '</label>' +
              '<a href="">-</a></div>'
                );              
    });                             

});

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dqm4977 2013-01-09 10:28
    关注
    <select id="my_select">
      {foreach from=$arrNetworkDTO item=NetworkDTO}
       <option value="{$NetworkDTO->name|escape}">{$NetworkDTO->name}</option>
      {/foreach}
    </select>
    
    <script>
      $("#my_select").change(function() {
        $(".addCardOVM").append("<div>" + $(this).val() + "</div>")
      });
    </script>
    

    Edit: To get the value of a selected option on button click:

    $('a.addListOVM').click(function(){
    
    
            $('.addCardOVM').append(
                  '<div class="divform"><label>' + $(".my_select:selected").val() + '</label>' +
                  '<a href="">-</a></div>'
                    );
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记