【发布时间】:2017-03-12 16:45:47
【问题描述】:
我有多个下拉菜单,它们的名称如下:
pricing[1], pricing[2], pricing[3] 等等。
onchange 每个名为 pricing[] 的下拉列表的事件,我想获取 pricing[x] 的索引,在这种情况下为“x”,以及选定的值。
HTML:
foreach($allindichapter as $chapter){
echo "<select name='pricing[".$chapter['CHAPTER']."]' id='pricing[".$chapter['CHAPTER']."]' class='pricing' style='width:100%'>";
foreach($indimonthly as $imchapter => $type){
foreach($type as $typ => $price){
if($imchapter == $chapter['CHAPTER'])
echo '<option value="'.$chapter.'">Monthly - '.$price.'Php</option>';
break;
}
}
echo "</select>";
}
问题是,我什至无法为下拉菜单输入onchange 函数。我尝试了不同的方法,例如:
$('select').change(function(){
console.log($('option:selected',this).index());
});
$(function(){
$('select').change(function(){
console.log($('option:selected',this).index());
});
$('.pricing').bind("change",function(){
var id = $(this).attr('id');
alert(id);
});`
我还使用了带有 JQuery 函数的 onchange="pricechange()"。
任何帮助将不胜感激。
作为参考,这是我想要实现的目标:
当定价发生变化时,我会更新持续时间下拉菜单。
【问题讨论】:
-
如果这些都不起作用,您还必须发布 HTML,并说明您是如何创建这些选择的
-
谢谢。我已经添加了我如何创建下拉菜单的完整代码。
-
控制台显示什么错误?
-
看起来所有这些都应该与发布的标记一起使用,所以还有其他问题 -> jsfiddle.net/3ooaagj6
-
您的选择元素是动态创建的吗? ajax 调用?
标签: javascript jquery html html-select dropdown