javascript - HTML SELECT - Run script onchange from option to another - Stack Overflow

I have the following :<selectid="p1" name="p1"><option value="ok&qu

I have the following :

<select  id="p1" name="p1">
  <option value="ok">Ok</option>
  <option value="closed">Closed</option>
  <option value="ko">KO</option>
</select>

I'm wondering how can i run a script on the page only when the value is changed from 'Ko to Ok'.

I have the following :

<select  id="p1" name="p1">
  <option value="ok">Ok</option>
  <option value="closed">Closed</option>
  <option value="ko">KO</option>
</select>

I'm wondering how can i run a script on the page only when the value is changed from 'Ko to Ok'.

Share Improve this question edited May 23, 2014 at 11:28 Emanuel Vintilă 1,9494 gold badges25 silver badges37 bronze badges asked May 23, 2014 at 11:25 PaxBinPaxBin 1113 silver badges14 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

You can bind change event to your select. You would need to bind the event when the p1 is added to dom for that you can use document.ready

Live Demo

$(document).ready(function(){
    $('#p1').change(function(){
         alert(this.value);
    });
});

Edit To pare it with last selection

Live Demo

prevVal = "";
$('#p1').change(function(){    
    if(prevVal === "ko" && this.value === "ok")
        alert("from ko to ok");
     prevVal = this.value;
});

you have to store previous value and on change check what is the previous value and what is current one like this:

$(document).ready(function(){

    var previousvalue = "";
    $('#p1').on('change',function(){

        if(previousvalue === "ko" && $(this).val() === "ok")
        {
            alert("KO to OK");
        }

        previousvalue = $(this).val();

    })

})

DEMO FIDDLE

you can also put onchange="your_function()" in select element

<select onchange="your_function()">
    <option></option>
</select>

I think so this should work for you

var prev_val = null;
$('#p1').change(function(){
   if(prev_val == "Ko" && this.value == "Ok"){//do something}
   prev_val = this.value
});

Use Jquery .change()

Try this :

<select  id="p1" name="p1">
  <option value="ok">Ok</option>
  <option value="closed">Closed</option>
  <option value="ko">KO</option>
</select>

Jquery:

$('select#p1').change(function(){
  if(this.value == "ok"){
     //-- Your Code IF someone select "OK"
  }
  else if(this.value == "closed"){
    //-- Your Code IF someone select "Closed"
  }
  else if(this.value == "ko"){
    //-- Your Code IF someone select "KO"
  }
});

According to OP question (run a script on the page only when the value is changed from 'Ko to Ok'):

You can use Custom Data Attributes for storing the previously selected option value.

Try this:

$("select#p1").one('focus', function () {
      $(this).prop('data-previous', $('select#p1').val());
}).change(function(){        
    if($(this).prop('data-previous') == "ko" && $(this).val() == "ok"){
       alert('Change');
    }
    $(this).prop('data-previous', $(this).val());
});

Working Example

Above code - We set data attribute data-previous when the select drop-down focused once. and than we bind the .change() event with select, so when options changed occurred we can check the previous selected and currently selected value and perform our operation (in the above example i am doing alert()).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744050449a4549977.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信