jQuery select2. Can't get focus in the input f

2019-08-30 12:42发布

问题:

<select id="e1" multiple="multiple" style="width:300px">
    <option value="AL">Alabama</option>
    <option value="WY">Wyoming</option>
</select>

$("#e1").select2({
    formatSelection:function(state){
        return "<input type=\"text\" value=\""+1+"\" class=\"quantity\"/>" + state.text;
    },
});

Example: http://jsfiddle.net/Gm668/

回答1:

You could try the following code:

$(document).ready(function() { 

    $("#e1").select2({
        formatSelection:function(state){
           return "<input type=\"text\" value=\""+1+"\" class=\"quantity\"/>" + state.text;
        }
    }).on("select2-focus", function(e) {
        $(".quantity").focus(); 
    });

});