JQuery detect dropdown value is selected even if it's not changed

前端 未结 7 1763
感动是毒
感动是毒 2021-01-05 01:29

Using JQuery or JavaScript, I want to detect when a user selects a value, even if they don\'t change the already selected value.

How can this be accomplished?

<
7条回答
  •  不知归路
    2021-01-05 02:25

    I've made a working jsfiddle only tested on firefox. I've managed to do what you want by adding a dummy option to the selectbox. This dummy option has a display:none style and won't be visible in the option list.

    DEMO

    
    
    
    
    $('#myselect').on('focus',function(){    
        $(this).find("option.dummy").prop("selected", true);
    });
    
    $('#myselect').on('change',function(){
        var select=$(this);
        var text=select.find("option:selected").text();
    
        $('.dummy').attr('value',select.val()).text(text);
        alert(select.val());
    
    
    });
    

提交回复
热议问题