最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - drop down onchange selected list not working? - Stack Overflow

programmeradmin3浏览0评论

Drop down not working when I select same option second time. This is my code:

<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>

First time select any care eg:Audi, then it alerts Audi, after alert again select Audi then there is no alert ing. Could anybody help me whats wrong in this?

Drop down not working when I select same option second time. This is my code:

<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>

First time select any care eg:Audi, then it alerts Audi, after alert again select Audi then there is no alert ing. Could anybody help me whats wrong in this?

Share Improve this question edited Oct 30, 2014 at 6:19 JoriO 1,0486 silver badges13 bronze badges asked Oct 30, 2014 at 5:45 Gangadhar BGangadhar B 1091 gold badge3 silver badges13 bronze badges 7
  • seem to be working fine here.. jsfiddle/s2n15ydo – Abdul Jabbar Commented Oct 30, 2014 at 5:47
  • select BMW shows alert,again select BMW then observe? – Gangadhar B Commented Oct 30, 2014 at 5:51
  • @AbdulJabbar,you got it what i'm trying to say? – Gangadhar B Commented Oct 30, 2014 at 5:57
  • do you want it to fire everytime you select some value.. even if its already selected? – Abdul Jabbar Commented Oct 30, 2014 at 6:04
  • @AbdulJabbar,obsolutely yes – Gangadhar B Commented Oct 30, 2014 at 6:11
 |  Show 2 more ments

6 Answers 6

Reset to default 2

Since you select the same option TWICE so there is no change in selected object of Dropdown.

Try like below

Updated Answer

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;              
}

Fiddle is HERE

The event is onchange when you try to select the item already selected change event wont fire.

Are you looking for this??

Add the following code

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

Unfortunately, the above method will not work in "Chrome", try in "Internet Explorer" or "Mozialla" (atleast IE is being used for this reason :p )

you forgot to close option tags and to do this you need to attach onclick event handler to all of the options in the dropdown, see below:

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>

Update with Demo : http://jsbin./hujavalayu/1/edit

I hope this will satisfy your requirement, Please check the link

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>

you should use onclick="this.value=null" and define your onchange as well, so every time you select any option it will clear the selected item and you can select same option again ;). Here is the sample Code:

$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);
}
发布评论

评论列表(0)

  1. 暂无评论