代码之家  ›  专栏  ›  技术社区  ›  helgatheviking

关于WordPress元框输入字段的jquery

  •  1
  • helgatheviking  · 技术社区  · 16 年前

    我在我的WordPress主题中有很多元框。单击w/class=“meta_upload_button”按钮,我想启动默认的媒体上载程序。多亏了一些在线教程,我大部分时间都在学习这个部分。我正在尝试使代码适应多个输入/按钮对。

    <input type="text" />
    <button type="button" class="meta_upload_button">Upload</button>
    

    ID、名称和值都是由wpachemy metabox类动态生成的。

    问题是我的代码改变了最后一个文本输入字段的值,不管我点击哪个按钮,这让我觉得这是一个jquery选择问题。但是,我使用了我的花哨的颜色调试方法,并对单击按钮之前的输入框应用了边框颜色。那就行了。但是在send-to-u编辑器函数中,同一声明没有选择正确的输入。

    var ww = $('#post_id_reference').text();
    
    $('.meta_upload_button').each(function() {
    
     var button = $(this);
    
     $(this).click(function() {
    
      //test- result= changes border color on correct input
            button.prev('input').css("border","3px solid red");
    
    
      window.send_to_editor=window.send_to_editor_clone;
      tb_show('', 'media-upload.php?post_id=' + ww + '&type=image&TB_iframe=true');
      return false;
     });
    
      //my version of the send_to_editor
     window.send_to_editor_clone = function(html){
            //grabs the URL of the selected image
      imgurl = $('img',html).attr('src');
    
      forminput = button.prev('input');
    
            //changes the value on the LAST input 
      forminput.val(imgurl);
      tb_remove();
     }
    
    }); //end each
    
    2 回复  |  直到 16 年前
        1
  •  0
  •   forefinger    16 年前

    好吧,我想我知道问题出在哪里了。

    $('.meta_upload_button').each
    

    为每个按钮运行一次函数体。每次运行时,变量

    window.send_to_editor_clone
    

    设置为某个应填充文本字段的函数。但是,只有一个 窗口 如此 window.send_to_编辑器 将等于它被分配的最后一个值。在这种情况下,它的功能是 按钮 最后一个值,也就是最后一个按钮,这是您描述的行为。

    你怎么修理它?我不太确定。你是不是只能通过 窗口。发送到编辑器(HTML) ?理想情况下,您只定义一次该函数,但将特定的按钮作为参数传递给它。

    到处搜索 森德托托编辑器 对如何处理这件事我不太了解。您可以按以下方式进行操作:

    var ww = $('#post_id_reference').text();
    
    var which_button;
    
    $('.meta_upload_button').each(function() {
    
     var button = $(this);
    
     $(this).click(function() {
       which_button = button;    
    
    
      tb_show('', 'media-upload.php?post_id=' + ww + '&type=image&TB_iframe=true');
      return false;
     });
    }); //end each
    
    //my version of the send_to_editor
    window.send_to_editor_clone = function(html){
      //grabs the URL of the selected image
      imgurl = $('img',html).attr('src');
    
      forminput = which_button.prev('input');
    
      //changes the value on the LAST input 
      forminput.val(imgurl);
      tb_remove();
    }
    window.send_to_editor=window.send_to_editor_clone;    
    

    只有当 window.send_to_编辑器 在单击处理程序之后调用。否则,它会变得更棘手。我认为,使用setTimeout是可能的,但到那时它变成了一个并发的东西,这是很难考虑的。

        2
  •  0
  •   helgatheviking    16 年前

    @食指-非常感谢!我现在使用每种方法的问题都是有意义的,我永远不会看到这一点。我也会发誓我尝试过类似于哪个按钮的方法,但是我今天醒来,看到了你的答案——然后砰的一声,成功了!

    var ww = $('input[name=post_ID]').val();
    
    $('.meta_upload_button').each(function() {
    
        var button = $(this);
    
        $(this).click(function() {
            which_button = button;
            window.send_to_editor=window.send_to_editor_clone;
            tb_show('', 'media-upload.php?post_id=' + ww + '&type=image&TB_iframe=true');
            return false;
        });
    
    
        window.send_to_editor_clone = function(html){
            imgurl = $('img',html).attr('src');
            forminput = which_button.prev('input');
            forminput.val(imgurl);
    
            tb_remove();
        }
    
    }); //end each
    

    send_to_editor是另一个几乎没有任何文档的wordpress函数。但在上面,我需要将克隆的send_保存在click函数中,以便只在我的元数据库中的click按钮上调用它,而不会干扰常规日志上的media按钮。

    推荐文章