Eventos de selección múltiple de jQuery UI

Estoy usando este Multiselect jquery component. and I can't figure how to trigger and subscribe to the events of this thing. (I'm new to js).

What I've been trying so far is to trigger a collectionChanged event in the _updateCount function of the ui.multiselect.js file:

_updateCount: function() {
        this.selectedContainer.find('span.count').text(this.count + " " + $.ui.multiselect.locale.itemsCount);

        //How do I subscribe to the event?
        $(this).trigger('collectionChanged');
    }

Then I'm trying to subscribe to the 'collectionChanged' event from asp.net:

  $('#<%= dropDown.ClientID %>').bind('collectionChanged', function ()
     { 
         alert("Changed!");
     });

The generated page markup is this:

<select name="ctl06$dropDown" id="ctl06_dropDown" class="multiselect" multiple="multiple" name="countries[]">
        <option value="1134">A</option>
        <option value="1980">B</option>
        <option value="17789">C</option>
        <option value="180367">D</option>
        <option value="1990673">E</option>

    </select>

 <script type="text/javascript">
 $(function () { $('.multiselect').multiselect(); });

 $('#ctl06_dropDown').bind('collectionChanged', function ()
 {   
     //Not working (never triggerd)
     alert("Changed!");
 });

</script>

Gracias.

preguntado el 04 de julio de 12 a las 07:07

2 Respuestas

El valor de this al _updateCount function is not a DOM element. For what you are trying to do, if I understand you well, you need to trigger your custom event in a DOM element and then listen to that custom event on the same DOM element. So you have two problems on your code: the first one is that you are trying to trigger an event using $(this) y thisis not what you expect, and the second one is that you are listening to an event on a DOM element that never triggers that event. If you do something like:

_updateCount: function() {
    this.selectedContainer.find('span.count').text(this.count + " " + $.ui.multiselect.locale.itemsCount);
    $("#ctl06_dropDown").trigger("collectionChanged");
}

Entonces tu linea $('#ctl06_dropDown').bind('collectionChanged'... debería funcionar bien.

Respondido 04 Jul 12, 07:07

the plugin you are using doesnt support events.

use the next version of it. http://quasipartikel.at/multiselect_next/

if has events explained with sample code at http://quasipartikel.at/multiselect_next/#optionTabs-3

You need to use both the events called "selected" & "deselected".

Respondido 04 Jul 12, 07:07

Thanks codebins, with victor's answer I can now trigger some basic events. but anyway, it is good to know there is a better way to do this with this "next version". - Receta de limo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.