duanke6057 2011-03-13 13:27
浏览 17

div中的jQuery文本在admin中输入

One page I have articles with prices. I would like to do administration. When I am administrator and I would click on price 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

  • 写回答

1条回答 默认 最新

  • duanbo7517 2011-03-13 13:33
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)