开发者

JQuery .bind() correct format

开发者 https://www.devze.com 2023-04-04 03:00 出处:网络
I have this code - jQuery(\'.wymeditor\').wymeditor( { html: \'<p>Hello, World!<\\/p>\', postInit: function(wym) {

I have this code -

jQuery('.wymeditor').wymeditor( {
    html: '<p>Hello, World!<\/p>',
    postInit: function(wym) {
         var html = "<li class='wym_tools_newbutton'>"
                 + "<a name='NewButton' href='#'"
                 + " style='background-image:"
                 + " url(js/wymeditor/img/media.png);'"
                 + " title='Insert an image' >"
                 + "</a></li>";
       jQuery(wym._box).find(wym._options.toolsSelector + wym._options.toolsListSelector).append(html);
       jQuery(wym._box).find('li.wym_tools_newbutton a').click(function() {

       jQuery('#modal').show().css( { 'left': (winW-980)/2+'px', 'top': '100px' } ).load('admin_list_media.php');

                jQuery('.imgname').bind('change',function(){

                alert('123');

                var InsertImg = '#'+jQuery(this).attr('id');

                wym.insert('<img src="uploads/'+jQuery(InsertImg).val()+'" />');

                jQuery('#modal').empty().hide();

             });
            return(false);
            } );
            }
        } );

which adds a new button to a wym editor. This opens a modal which contains images, the idea is to select an image and insert into the wym editor. Works if i use jQuery('.imgname').live('change',function(){ ... but not if I use jQuery('.imgname').bind('change',function(){开发者_JAVA技巧 problem is I have to use .bind() because the change event handler gets bound every time the modal opens and so I dupliacte image inserts each time I was told to replace .live() with .bind() but it doesn't work (OK in my code). Suggestions please


First of all: why don't you use live but do it once when your page loads? You say "I have to use bind because the event handler gets bound every time..." but that sounds confusing. The event handler gets bound every time because you use bind, not the other way around.

Failing that, a quick and dirty solution would be:

$(".imgname").unbind("change").bind("change", function () {
    // ...
});

A much better solution (again, without live) would be the hasEventListener plugin. Read the documentation, or play with its sandbox on JSFiddle.


Solved it by moving the insert outside of the onchange section

0

精彩评论

暂无评论...
验证码 换一张
取 消