简体   繁体   English

使用select2并将事件附加到它

[英]using select2 and attaching event to it

I am using a simple select2 where in the options i am passing a common class and i need to attach an event that on every change or click of item in the option, it should trigger some jquery event. 我正在使用一个简单的select2,其中在选项中我传递了一个普通类,并且我需要附加一个事件,该事件在选项中的每个更改或单击项目时都应触发一些jquery事件。 i have the code like this: 我有这样的代码:

<select name="transferfrom" id="transferfrom" data-placeholder="Please choose one" data-rel="chosen">
        <option value=""></option>  

            <option class="locDetails" id="1" value="1">Main Store</option>

            <option class="locDetails" id="2" value="2">Branch Store A</option>

            <option class="locDetails" id="3" value="3">Branch Store B</option>

            <option class="locDetails" id="4" value="4">Branch Store C</option>

            <option class="locDetails" id="5" value="5">Branch D</option>

    </select>

if i see firebug data for the above, i see he code as: 如果我看到上面的萤火虫数据,我会看到他的代码为:

<div class="controls">
    <div class="select2-container" id="s2id_transferfrom" title=""><a tabindex="-1" class="select2-choice" href="javascript:void(0)">   <span class="select2-chosen" id="select2-chosen-3">Main Store</span><abbr class="select2-search-choice-close"></abbr>   <span role="presentation" class="select2-arrow"><b role="presentation"></b></span></a><label class="select2-offscreen" for="s2id_autogen3"></label><input type="text" role="button" aria-haspopup="true" class="select2-focusser select2-offscreen" aria-labelledby="select2-chosen-3" id="s2id_autogen3"></div><select data-rel="chosen" data-placeholder="Please choose one" id="transferfrom" name="transferfrom" title="" class="select2-offscreen" data-bv-field="transferfrom" tabindex="-1">
        <option value=""></option>  

            <option value="1" id="1" class="locDetails">Main Store</option>

            <option value="2" id="2" class="locDetails">Branch Store A</option>

            <option value="3" id="3" class="locDetails">Branch Store B</option>

            <option value="4" id="4" class="locDetails">Branch Store C</option>

            <option value="5" id="5" class="locDetails">Branch D</option>

    </select><i style="display: none; top: 0px;" class="form-control-feedback" data-bv-icon-for="transferfrom"></i>
    </div>

here is my jquery event what i am trying to do on change of option item or click of option item 这是我的jquery事件,我要在更改选项项或单击选项项时要做的事情

$(document).on('change click','#transferfrom option.locDetails',function() {
        var ID = $(this).val();
        alert(ID);
        $(".reloadtransferform").load('transferfrom.cfm?id='+id+'&cache='+Math.random());
    });

but it does seems to work 但这确实有效

Try this : attach change event to select box instead of option s. 尝试以下操作:将更改事件附加到select框而不是option s。

NOTE - Please make sure that transferfrom is unique id through out the html document. 注意 -请确保整个HTML文档中的transferfrom是唯一ID。

$(document).on('change','#transferfrom .select2-offscreen',function() {
      var ID = $(this).val();
      alert(ID);
      $(".reloadtransferform").load('transferfrom.cfm?id='+id+'&cache='+Math.random());
});

Try this: FIDDLE 试试这个: FIDDLE

$("#transferfrom").on("change", function(e) { 
    var ID = e.val;
    alert(ID);
    $(".reloadtransferform").load('transferfrom.cfm?id='+ID+'&cache='+Math.random());
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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