WordPress 元框输入字段上的 jquery
我的 WordPress 主题中有一堆元框。单击带有 class="meta_upload_button" 的按钮时,我想启动默认媒体上传器。感谢一些在线教程,我已经使该部分大部分工作了。我正在尝试调整代码以适用于多个输入/按钮对。
<input type="text" />
<button type="button" class="meta_upload_button">Upload</button>
id、名称和值均由 WPAlchemy MetaBox 类动态生成。
问题是,无论我单击哪个按钮,我的代码都会更改最后一个文本输入字段的值,这让我认为这是一个 jquery 选择问题。但是,我使用了我奇特的颜色调试方法,并将边框颜色应用于单击按钮之前的输入框。效果很好。但在 send_to_editor 函数中,相同的声明没有选择正确的输入。
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
i have a bunch of meta boxes in my wordpress theme. on the click of a button w/ class="meta_upload_button" i want to launch the default media uploader. thanks to some online tutorials I have that part mostly working. i am trying to adapt the code to work for multiple input/button pairs.
<input type="text" />
<button type="button" class="meta_upload_button">Upload</button>
ids, names and values are all dynamically generated by the WPAlchemy MetaBox class.
the problem it that my code is changing the value on the last text input field no matter which button i click on, which makes me think it is a jquery selection problem. however i used my fancy color debugging method and applied a border color to the input box that precedes the button clicked. that works just fine. but in the send_to_editor function the same declaration doesn't select the right input.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我想我明白问题所在了。
为每个按钮运行一次函数主体。每次运行时,变量
都会被设置为某个应该填充文本字段的函数。但是,只有一个
window,因此window.send_to_editor将等于最后分配的值。在本例中,它是 button 具有其将具有的最后一个值的函数 - 即,它是最后一个按钮,这就是您所描述的行为。
你如何解决它?我不太确定。您是否只能通过 window.send_to_editor(html) 访问此内容?理想情况下,您只需定义该函数一次,但将特定按钮作为参数传递给它。
谷歌搜索 send_to_editor 并没有告诉我如何处理这个问题。您可以按如下方式执行此操作:
仅当在点击处理程序之后调用 window.send_to_editor 时,此操作才有效。不然处理起来会更麻烦。我认为使用 setTimeOut 是可能的,但到那时它就变成了并发的事情,这是更难考虑的。
OK, I think I see what the problem is.
runs the body of the function once for each button. Every time it runs, the variable
gets set to some function which is supposed to populate the text field. But, there is only one
window, so window.send_to_editor is just going to be equal to the last value it was assigned. In this case, it's the function where button has the last value it's going to have - i.e., it's the last button, which is the behavior you described.
How do you fix it? I'm not really sure. Are you stuck with only being able to access this thing through window.send_to_editor(html)? Ideally you would only define that function once, but have the particular button passed into it as a parameter.
Googling around for send_to_editor doesn't tell me much about how to deal with this. You might be able to do it as follows:
This would only work if window.send_to_editor gets called after the click handler. Otherwise, it gets much nastier to deal with. It would be possible with a setTimeOut, I think, but at that point it becomes a concurrency thing, which is harder to think about.
@forefinger - 非常感谢你!我对每个的使用的问题现在是有意义的,我永远不会看到这一点。我也发誓我尝试过类似于which_button的东西,但我今天醒来,看到了你的答案——而且它成功了!
send_to_editor 是另一个几乎没有任何文档的 WordPress 函数。但上面,我需要将克隆的 send_to_editor 保留在单击函数中,以便仅在单击元框中的按钮时调用它,并且不会干扰常规帖子上的媒体按钮。
@forefinger - thank you so much! the problem w/ my use of each makes sense now and i would never have seen that. i also would have sworn that i'd tried something similar to which_button, but i woke up today, saw your answer- and boom it worked!
send_to_editor is another of those wordpress functions that barely has any documentation. but above, i need to need to keep the cloned send_to_editor in the click function so that it is only called on clicking buttons in my metaboxes and does not interfere w/ the media buttons on the regular post.