下拉onchange所选列表不起作用?

问题描述 投票:2回答:6

第二次选择相同选项时,下拉菜单不起作用。这是我的代码:

<html>
    <body>
        <p>Select a new car from the list.</p>
        <select id="mySelect" onchange="myFunction()">
            <option value="Audi">Audi
            <option value="BMW">BMW
            <option value="Mercedes">Mercedes
            <option value="Volvo">Volvo
        </select>
        <script>
            function myFunction() {
                var x = document.getElementById("mySelect").value;
                alert(x);
            }
        </script>
    </body>
</html>

[第一次选择任何护理,例如:奥迪,然后警告奥迪,再次警告后选择奥迪,则没有警告。有人可以帮我这有什么问题吗?

javascript html drop-down-menu
6个回答
2
投票

由于您选择了相同的选项TWICE,所以selectedDropdown对象没有变化。

尝试如下

更新后的答案

var dd = document.getElementById('mySelect');          
var storeLstSlct = document.getElementById('checkIndx');
var slctdValue = '';   

if(dd.selectedIndex == 0)
{
    return false;
}else if(storeLstSlct.value == dd.options[dd.selectedIndex].value)
{              
    storeLstSlct.value = 'garbage'; 
    return false;              
}else
{                            
    slctdValue = dd.options[dd.selectedIndex].value;    
  alert(slctdValue); 
    storeLstSlct.value = slctdValue;              
}

小提琴是HERE


0
投票

[当您尝试选择已选择的项目时,事件为onchange事件,将不会触发。


0
投票

您是否在寻找this??

添加以下代码

    $('option').click(function(){  
       alert($(this).text());
    });

不幸的是,以上方法在“ Chrome”中不起作用,请在“ Internet Explorer”或“ Mozialla”中尝试(由于这个原因,至少使用了IE:p)


0
投票

您忘了关闭选项标签,为此,您需要将onclick事件处理程序附加到下拉菜单中的所有选项,请参见下文:

var x = document.getElementById("mySelect");

//attach onclick event handlers to options
for (var i = 0; i < x.options.length; i++) 
{
  if (x.options[i].addEventListener) 
  { 
    /*all other browsers*/
    x.options[i].addEventListener("click", function() {mysFunction(this)}, false);
  } 
  else if (x.options[i].attachEvent) /*ie < 9*/ 
  {
    x.options[i].attachEvent("click", function() {mysFunction(this)});
  }
}

//execute my alert box on item select
function mysFunction(elem) {
  alert(elem.value);
}
<select id="mySelect">
  <option value="Audi">Audi</option>
  <option value="BMW">BMW</option>
  <option value="Mercedes">Mercedes</option>
  <option value="Volvo">Volvo</option>
</select>

0
投票

使用演示更新: http://jsbin.com/hujavalayu/1/edit

我希望这会满足您的要求,请检查链接

Trigger the event when selected the same value in dropdown?

<p>Select a new car from the list.</p>
<select id="ddList" onClick="onSelect()">
    <option value="0">Select Me</option>
    <option value="List1">List1</option>
    <option value="List2">List2</option>
    <option value="List3">List3</option>
</select>


<script>
    var prevIndex = "";

    function onSelect() {
        var currIndex = document.getElementById("ddList").selectedIndex;
        if (currIndex > 0) {
            if (prevIndex != currIndex) {
                alert("Selected Value = " + document.getElementById("ddList").value);
                prevIndex = currIndex;
            } else {
                prevIndex = "";
            }
        }
    }
</script>

0
投票

您应该使用onclick="this.value=null"并同时定义onchange,因此,每次选择任何选项时,它都会清除所选的项目,然后您可以再次选择相同的选项;)。这是示例代码:

$serial_no = 0;

<select id="<?php echo $serial_no++; ?>" onclick="this.value=null" onchange="update_value(this.value,this.id)" class="form-control" required>
  <option><?php echo $row['card']; ?></option>
  <option>---------</option>
  <option>Pending</option>
  <option>Deliver</option>
</select>

update_value(item){
  alert(item);
}
© www.soinside.com 2019 - 2024. All rights reserved.