[英]PHP foreach loop, unique <select> id for getElementById
What I am interested in getting is $option_value['price'] in a string from the dropdown, not . 我感兴趣的是下拉列表中的字符串中的$ option_value ['price'],而不是。 Using .value returns the later.
使用.value将返回更高的值。
I have select menus that are created in a foreach() loop. 我有在foreach()循环中创建的选择菜单。 The drop downs will hold price options.
下拉菜单将保留价格选项。 I am trying to give each product it's own id so I can use the javascript onChange() function to update prices on the screen.
我正在尝试给每个产品提供自己的ID,以便可以使用javascript onChange()函数更新屏幕上的价格。
The select tag looks like this: select标签如下所示:
<select name="option[<?php echo $option['product_option_id']; ?>]" class="select_options" id="select_<?php echo $product['product_id']; ?>" onchange="getIt(this);">
and the javascript: 和javascript:
function getIt(el) {
var id = el.id;
a = document.getElementById(id).text;
alert(a);
}
Alerting out id gives me select_some number. 通知ID给我select_some数字。 So I can see that it is getting the id.
因此,我可以看到它正在获取ID。 but alerting out a gives me [object HTMLSelectElement].
但是发出警告会给我[object HTMLSelectElement]。
so I tried 所以我尝试了
alert(a.options.selectedIndex.value);
and get unidentified. 并变得不明身份。
<?php if ($product['options']) { ?>
<div class="options" id="option_<?php echo $product['product_id']; ?>">
<?php foreach ($product['options'] as $option) { ?>
<?php if ($option['type'] == 'select') { ?>
<div id="option-<?php echo $option['product_option_id']; ?>" class="option select_option">
<select name="option[<?php echo $option['product_option_id']; ?>]" class="select_options" id="select_<?php echo $product['product_id']; ?>" onchange="getIt(this);">
<option value=""><?php echo $text_select; ?></option>
<?php foreach ($option['option_value'] as $option_value) { ?>
<option value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?>
<?php if ($option_value['price']) { ?>
(<?php echo $option_value['price_prefix']; ?><span id="newPrice"><?php echo str_replace('.00','.',$option_value['price']); ?></span>)
<?php } ?>
</option>
<?php } ?>
</select>
</div>
<?php } ?>
Change : 变更:
a = document.getElementById(id).text;
To: 至:
a = document.getElementById(id).value;
That will alert the value of selected option. 这将提醒所选选项的值。
Try this code, put document.getElementById(id).value
instead of document.getElementById(id).text
尝试使用此代码,将
document.getElementById(id).value
代替document.getElementById(id).text
function getIt(id) {
a = document.getElementById(id).value;
alert(a);
}
"is there a way I can get the text shown on the drop down ($xx.xx), instead of the value?"
“有没有办法让我获得下拉列表($ xx.xx)上显示的文本而不是值?”
I think this is what you are looking for: 我认为这是您要寻找的:
function getIt(el) {
var a = el.options[el.selectedIndex].text;
alert(a);
}
el.selectedIndex
gives you, obviously, the index of the currently selected option, which you can use as an index into the collection of options, el.options
, and then get that option's text
. 显然,
el.selectedIndex
为您提供当前所选选项的索引,您可以将其用作索引选项el.options
,然后获取该选项的text
。 The text
property belongs to each option, not to the select. text
属性属于每个选项,而不属于select。
Demo: http://jsfiddle.net/FCnUQ/ 演示: http : //jsfiddle.net/FCnUQ/
(Note that if no option is selected selectedIndex
will be -1
.) (请注意,如果未选择任何选项,则
selectedIndex
将为-1
。)
You don't need to use getElementById()
at all because you already have a reference to the element, el
. 您根本不需要使用
getElementById()
,因为您已经有了对元素el
的引用。
as you passing this like getit(this)
you should code like this 当您像
getit(this)
这样传递代码时,应该这样编码
function getIt(el) {
var id = el.id;
a = document.getElementById(id).value;
alert(el.value); //you can directly alert like this
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.