简体   繁体   中英

Select2 onchange not working

When I've added select2 to all selects in my app it worked very nice except when in my select is this property(it works when I don't use select2):

onchange="'refresh()'"

Function refresh:

function refresh()
{
    document.forms[0].submit();
}

This is how I run select2

    $("select").each(function(){
        var this1 = $(this);
        if(this1.attr('multiple')!='multiple')
        {
            this1.select2();
        }
    });

How to pass this? Or maybe there is some mechanism inside the select2 that deals with that kind of problems? All kinds of suggestions are welcome:D

you need to update this: $(#Select/Input Element).select2();

 $("#select/input element").change(function () { var valueToSet = $("#select/input element").select2('data').text; $('#hiddent field to which data to be set').val(valueToSet); }); 

Not sure to understand. When one of yours selects changes, you want to call the refresh method? In that case :

$("select").each(function(){
    var $this = $(this);
    if($this.attr('multiple')!='multiple'){
        $this.select2();
        $this.change(refresh);
    }
});

Remove single quote from onchange="'refresh()' . Try as mentioned below :

<input type="checkbox" onchange="Refresh();"/>

And your script will be defined as below :

<script type="text/javascript">

        function Refresh() {
            alert('refresh');
        }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM