使用 jQuery 实现 Facebook 风格的多个配置文件编辑

发布于 2024-10-20 17:02:14 字数 1112 浏览 4 评论 0原文

我正在尝试创建 facebook 风格的多个文本框编辑,但不起作用。

 var inputId = '';
 var that = '';
 var data = '';
 $(".text_wrapper").live('click', function() {
    that = this;
    data=$(this).html();    
    inputId = '#'+$(this).next().attr("id");
    $(inputId).val(data);
    $(inputId).show();
    $(that).hide();
    $(inputId).focus();
 });

 $(inputId).live("mouseover", function(e){
    $(inputId).hide();$(that).show();
 });

 $(inputId).change(function() {
    $(inputId).hide();
    var boxval = $(inputId).val();
    var dataString = 'data='+ boxval;
    $.ajax({
      type: "POST",
      url: "test.php",
      data: dataString,
      cache: false,
      success: function(html) {
        $(that).html(boxval);
        $(that).show();
      }
    });
 });

这是 html

    <div class="text_wrapper">1245</div><input id="3123" name="timeout" type="text" 
class="edit" size="20" value="" />

    <div class="text_wrapper">98745</div><input id="3122" name="timeout" type="text" 
class="edit" size="20" value="" />

提前谢谢..

I am trying to create facebook style multiple textbox edit, but is'nt working.

 var inputId = '';
 var that = '';
 var data = '';
 $(".text_wrapper").live('click', function() {
    that = this;
    data=$(this).html();    
    inputId = '#'+$(this).next().attr("id");
    $(inputId).val(data);
    $(inputId).show();
    $(that).hide();
    $(inputId).focus();
 });

 $(inputId).live("mouseover", function(e){
    $(inputId).hide();$(that).show();
 });

 $(inputId).change(function() {
    $(inputId).hide();
    var boxval = $(inputId).val();
    var dataString = 'data='+ boxval;
    $.ajax({
      type: "POST",
      url: "test.php",
      data: dataString,
      cache: false,
      success: function(html) {
        $(that).html(boxval);
        $(that).show();
      }
    });
 });

Here goes html

    <div class="text_wrapper">1245</div><input id="3123" name="timeout" type="text" 
class="edit" size="20" value="" />

    <div class="text_wrapper">98745</div><input id="3122" name="timeout" type="text" 
class="edit" size="20" value="" />

Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青芜 2024-10-27 17:02:14
 <div>
 <div class="text_wrapper">1245</div>
 <input id="3123" name="timeout" type="text" class="edit" size="20" value="" />
 <div>

 <div>
 <div class="text_wrapper">98745</div>
 <input id="3122" name="timeout" type="text" class="edit" size="20" value="" />
 </div>




   jQuery(function(){

       //stpe 1
      jQuery('.text_wrapper').bind('mouseover',function(e){
             var elm = $(e.target);
             elm.hide();                     
            elm.parents('div:first')
           .find('input[type=text]').val(elm.html()).show()focus();              
       });

     //step 2
     //you should use 'blur' here rather than 'mouseover'
     // i have chosen mouseover as u have specified this in ur codes
      $('.edit').live("mouseover", function(e){
          var elm=$(e.target);
          elm.hide();
         elm.parents('div:first').find('.text_wrapper')
         .html(elm.val()).show();
       });

    //step 3
    // i dont think 'change' is good option, choose some other
    $('.edit').bind('change',function (e) {
        var elm = $(e.target);
        elm.hide();
        var boxval = elm.val();
        var dataString = 'data=' + boxval;
        $.ajax({
            type: "POST",
            url: "test.php",
            data: dataString,
            cache: false,
            success: function (html) {
                $(that).html(boxval);
                $(that).show();
            }
        });
    });


   });
 <div>
 <div class="text_wrapper">1245</div>
 <input id="3123" name="timeout" type="text" class="edit" size="20" value="" />
 <div>

 <div>
 <div class="text_wrapper">98745</div>
 <input id="3122" name="timeout" type="text" class="edit" size="20" value="" />
 </div>




   jQuery(function(){

       //stpe 1
      jQuery('.text_wrapper').bind('mouseover',function(e){
             var elm = $(e.target);
             elm.hide();                     
            elm.parents('div:first')
           .find('input[type=text]').val(elm.html()).show()focus();              
       });

     //step 2
     //you should use 'blur' here rather than 'mouseover'
     // i have chosen mouseover as u have specified this in ur codes
      $('.edit').live("mouseover", function(e){
          var elm=$(e.target);
          elm.hide();
         elm.parents('div:first').find('.text_wrapper')
         .html(elm.val()).show();
       });

    //step 3
    // i dont think 'change' is good option, choose some other
    $('.edit').bind('change',function (e) {
        var elm = $(e.target);
        elm.hide();
        var boxval = elm.val();
        var dataString = 'data=' + boxval;
        $.ajax({
            type: "POST",
            url: "test.php",
            data: dataString,
            cache: false,
            success: function (html) {
                $(that).html(boxval);
                $(that).show();
            }
        });
    });


   });
颜漓半夏 2024-10-27 17:02:14

我更好的缩进显示至少一个问题...试试这个:

 $(".text_wrapper").live('click', function() {
    that = this;
    data=$(this).html();    
    inputId = '#'+$(this).next().attr("id");
    $(inputId).val(data);
    $(inputId).show();
    $(that).hide();
    $(inputId).focus();

    $(inputId).live("mouseover", function(e){
      $(inputId).hide();$(that).show();
    });

    $(inputId).change(function() {
      $(inputId).hide();
      var boxval = $(inputId).val();
      var dataString = 'data='+ boxval;
      $.ajax({
        type: "POST",
        url: "test.php",
        data: dataString,
        cache: false,
        success: function(html) {
          $(that).html(boxval);
          $(that).show();
        }
      });
   });

 });

注意:这仍然有严重的问题...每次点击时您都会添加事件处理程序...在设置它们之前需要清除它们(至少)。或者不要在单击事件中添加处理程序。

My better indenting shows at least one problem... try this:

 $(".text_wrapper").live('click', function() {
    that = this;
    data=$(this).html();    
    inputId = '#'+$(this).next().attr("id");
    $(inputId).val(data);
    $(inputId).show();
    $(that).hide();
    $(inputId).focus();

    $(inputId).live("mouseover", function(e){
      $(inputId).hide();$(that).show();
    });

    $(inputId).change(function() {
      $(inputId).hide();
      var boxval = $(inputId).val();
      var dataString = 'data='+ boxval;
      $.ajax({
        type: "POST",
        url: "test.php",
        data: dataString,
        cache: false,
        success: function(html) {
          $(that).html(boxval);
          $(that).show();
        }
      });
   });

 });

Note: This still has serious problems... on every click you are adding event handlers... they need to be cleared out before you set them up (at the least). Or don't add the handlers in the click event.

橘和柠 2024-10-27 17:02:14

我在代码中看到的一个非常明显的问题是,inputId 是在 div 上的单击处理程序中设置的。您不能使用该变量来跟踪点击次数。为什么不将事件绑定到编辑类输入框呢?实际上,我不知道你为什么要在这段代码中跟踪这些 inputId。

One very apparent problem I see with the code is that inputId is set in the handler for the click on the div. You cannot use that variable to track clicks. Why don't you bind events to the edit class input boxes instead? Actually, I have no idea why you are keeping track of those inputIds in this piece of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文