从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.
所以我需要追加和取消追加包含选择了哪个选项值的div,问题是 首先显示选择值的方法是via。 使用Smarty PHP模板的for-each循环,我无法找到有关如何使用它实现它的任何文档。 p>
我尝试过的方法: strong >正如您将在下面看到的,我尝试在模板文件中创建一个JavaScript变量,但它只返回所选第一个选项的值。 我无法弄清楚为什么我只得到从循环中取得的第一个值。 p>
例如,如果我在选择'A'和'B'中有两个项目,那么在选择时 值选项,我将始终只接收'A'作为值。 p>
Smarty模板代码: strong> p>
JQuery代码: strong> p>
感谢任何帮助。 p>
div>
&lt; select class =“my_select”name =“vlans _ {$ NetworkDTO-&gt; id} []”&gt;
{foreach from = $ arrNetworkDTO item = NetworkDTO name = arr}
&lt; option class = “addOvmCardList”id =“addNameDisk {$ strIdTpl}”value =“{$ NetworkDTO-&gt; name | escape}”&gt; {$ NetworkDTO-&gt; name}&lt; / option&gt;
{/ foreach}
&lt; / select&gt;
code> pre>
$('a.addListOVM ')。click(function(){
$(“。my_select”)。change(function(){
$('。addCardOVM')。append(
'&lt; div class =“ divform“&gt;&lt; label&gt;'+ $(this).val()+'&lt; / label&gt;'+
'&lt; a href =”“&gt; - &lt; / a&gt;&lt; / div&gt; '
);
});
});
code> pre>
<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>'
);
});