开发者

jQuery text in div to input in admin

One page I have articles with prices. I would like to do administration. When I am administrator and I would click on price开发者_如何学Python input field text would show.

My code for this:

$('#priceBig').click(function() {
    var originalelement = this;
    var dishID = this.id;
    var currentText = $.trim($(this).text())

    $(this).hide().before('<input class="input" id="'+dishID+'" style="padding:3px; text-align:left; font-size:17px; width:50px;"  type="text" value="'+currentText+'"/>');     

    $('.input').live('change', function() {
        var picaID = this.id;
        var price = $(this).val();
        var thisparam = this;

        $.post('<?= site_url('dish/changePicaBigPrice') ?>',{ picaID : picaID, price:price}, 
        function(data) { 
            $(thisparam).remove();  
            $(originalelement).text(price).fadeIn(1000);
        },'text');

    });

});

But this doesn't work because jQuery store all originalelement objects and every change of price change all originalelements which were clicked before...

I hope I was clear what I want to do.

I would like to click on price, change and one I go on other price


I believe my old answer won't work.

New tactics - save the old ID in an attribute on the new input you create, and use it in the success handler.

$('#priceBig').click(function() {
    var currentText = $.trim($(this).text())

    var input = $('<input />')
      .addClass('input')
      .attr('type', 'text')
      .attr('orig-id', $(this).attr('id'))
      .val(currentText)
      .bind('change', function () {
        var t = $(this);
        var price = t.val();
        var origID = t.attr('orig-id');

        $.post('<?= site_url('dish/changePicaBigPrice') ?>',{ picaID : origID, price:price},
        function(data) {
            t.remove();
            $('#' + origID).text(price).fadeIn(1000);
        },'text');
      });

    $(this).hide().before(input);
});

EDIT: fixed typo in var origID = t.attr('orid-id');

EDIT: change picaID to origID in the $.post parameters

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜