【问题标题】:Getting the index of a dropdown array on change event在更改事件上获取下拉数组的索引
【发布时间】: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


【解决方案1】:

因此,如果我理解正确,您需要select 的索引,而不是所选option 的索引。

在这种情况下,您必须查看 $(this).attr('name') 并从该字符串中提取索引。

或者,您可以将data 属性添加到仅具有索引的选择中,这比从名称中提取它更容易检索。

我举了一个例子来说明如何做到这两点。

还要确保如果选择是通过 Ajax 包含的,则不能使用 .bind.change 而是使用 .on

$('select').change(function(){
  alert(
    'Select name index: ' + $(this).attr('name').replace(/pricing\[(\d+)\]/, '$1') + 
    '\nSelect data index: ' + $(this).data('index') + 
    '\nValue: ' + $(this).val()
  );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="pricing[1]" data-index="1">
  <option value=""></option>
  <option value="1-1">1-1</option>
  <option value="1-2">1-2</option>
</select>
<select name="pricing[2]" data-index="2">
  <option value=""></option>
  <option value="2-1">2-1</option>
  <option value="2-2">2-2</option>
</select>
<select name="pricing[3]" data-index="3">
  <option value=""></option>
  <option value="3-1">3-1</option>
  <option value="3-2">3-2</option>
</select>

【讨论】:

  • 非常感谢。 $(this).attr('name')replace 函数正是我想要的。我现在可以操纵数据了。谢谢大家的帮助。
【解决方案2】:

使用findselect获取选中的选项,使用index获取选项的索引。

 console.log($(this).find('option:selected').index())

$('select').change(function() {
  console.log(
    'Name: ',this.name,
    'Value: ',this.value,
    'Index: ',$(this).find('option:selected').index()
  )
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="pricing[1]">
  <option value="1">1</option>
  <option value="2">2</option>
</select>
<select name="pricing[2]">
  <option value="1">1</option>
  <option value="2">2</option>
</select>
<select name="pricing[3]">
  <option value="1">1</option>
  <option value="2">2</option>
</select>

【讨论】:

  • 他肯定会。 select 不止一个,使用上下文确保它不会选择文档中的所有选项,而只会选择当前选择中的选项。
  • 非常感谢。这些代码有效并且非常有用。我只需要将另一个标记为答案,因为我的问题是如何检索名称中的索引。
  • @Maranatha 没问题 :)
【解决方案3】:

您可以使用 $("#dropdownexample").prop('selectedIndex')

即: html

<select id="dropdownexample">
    <option value="yes">Yes</option>
    <option value="no" selected="selected">No</option>
</select>

Javascript

alert($("#dropdownexample").prop('selectedIndex'));

我已经尝试过了,它似乎有效。

【讨论】:

  • 感谢您的努力。我相信这是为了获取所选值的索引,对吧?我想要得到的是下拉列表名称中的索引。例如,我有一个名为“pricing[1]”的下拉菜单,我将在那里获得索引 1。此外,我仍然不确定在哪里实现它,因为我无法访问下拉菜单的 OnChange 函数。
猜你喜欢
  • 2014-12-16
  • 1970-01-01
  • 1970-01-01
  • 2014-06-10
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多