我添加了第二个上传图像(doc\\u thumb\\u box)。它们彼此完全独立工作。但是,当添加第二个URL时,第一个URL(doc\\u uri\\u box)不再将URL的值粘贴到框中。相反both 如果框中有任何内容,则将变为空白。
是否有明显的矛盾之处?
public static function doc_thumb_box() {
global $post;
$thumb = get_post_meta( $post->ID, \'upload_image\', true );
function my_admin_scripts() {
wp_enqueue_script(\'media-upload\');
wp_enqueue_script(\'thickbox\');
wp_enqueue_script(\'my-upload\');
}
function my_admin_styles() {
wp_enqueue_style(\'thickbox\');
}
if (isset($_GET[\'page\']) && $_GET[\'page\'] == \'my_plugin_page\') {
add_action(\'admin_print_scripts\', \'my_admin_scripts\');
add_action(\'admin_print_styles\', \'my_admin_styles\');
}
echo \'Enter a URL or upload an image for the thumb.\';
echo \'<br />\';
echo \'<br />\';
echo \'<label for="upload_image">\';
echo \'<input id="upload_image" type="text" size="36" name="upload_image" value=" \' . $thumb . \'" />\';
echo \'<input id="upload_image_button" type="button" value="Upload Thumb" />\';
echo "<script type=\\"text/javascript\\">
jQuery(document).ready(function() {
jQuery(\'#upload_image_button\').click(function() {
formfield = jQuery(\'#upload_image\').attr(\'name\');
tb_show(\'\', \'media-upload.php?type=image&TB_iframe=true\');
return false;
});
window.send_to_editor = function(html) {
imgurl = jQuery(\'img\',html).attr(\'src\');
jQuery(\'#upload_image\').val(imgurl);
tb_remove();
}
});
</script>\\r\\n";
}
public static function doc_uri_box() {
global $post;
$uri = get_post_meta( $post->ID, \'wpa_upload_doc\', true );
echo "<style type=\\"text/css\\">
#edit-slug-box {
display: none;
}
</style>";
echo \'<p>Please provide the abosulte url of the file (including the <code>http://</code>):</p>\';
echo \'<input type="text" id="wpa_upload_doc" name="wpa_upload_doc" value="\' . $uri . \'" size="25" style="width:85%" />\';
echo \'<input class="button" id="upload_doc_button" type="button" value="Upload Publication" alt="Upload Publication" />\';
echo "<script type=\\"text/javascript\\">
jQuery(document).ready(function() {
jQuery(\'#upload_doc_button\').click(function() {
formfield = jQuery(\'#wpa_upload_doc\').attr(\'name\');
tb_show(\'Upload Publication\', \'media-upload.php?TB_iframe=1&width=640&height=263\');
return false;
});
window.send_to_editor = function(html) {
var docurl = jQuery(html).attr(\'href\');
jQuery(\'#wpa_upload_doc\').val(docurl);
tb_remove();
}
});
</script>\\r\\n";
}
UPDATE
对于
doc_uri_box()
方法,我发现
window.send_to_editor
永远不会执行代码块!真奇怪。。
SO网友:Trip
所以这里有一些不必要的变量。
我不需要定义formfield
作为var,因为它未使用。我定义了两次因为有两个函数共享公共变量,所以最好嵌套window.send_to_editor
在每个单击功能中
This is the final version of both the jQuery functions ( the source of the problem)
jQuery(\'#upload_doc_button\').live(\'click\', function() {
window.send_to_editor = function(html) {
var docurl = jQuery(html).attr(\'href\');
jQuery(\'#wpa_upload_doc\').val(docurl);
tb_remove();
}
tb_show(\'Upload Publication\', \'media-upload.php?TB_iframe=1&width=640&height=263\');
return false;
});
jQuery(\'#upload_image_button\').click(function() {
window.send_to_editor = function(html) {
imgurl = jQuery(\'img\',html).attr(\'src\');
jQuery(\'#upload_image\').val(imgurl);
tb_remove();
}
tb_show(\'\', \'media-upload.php?type=image&TB_iframe=true\');
return false;
});