我想创建一个数组以便使用多个唯一的ID。我想我需要使用一个foreach(或者可能只是每个())语句来使这个代码工作,但它仍然没有。我已经尝试了两种。我的语法错了,还是使用了错误的方法来完成这项工作。 if语句正在使用单个var对象正常工作。我希望我清楚地说明这一点,我是jQuery的新手。我还应该提到,这是一堆Wordpress自定义元框字段。此外,如果BTNID和TXTID是单个对象变量,这可以正常工作。在jQuery中使用带有点击功能的foreach的正确语法?
jQuery(document).ready(function($){
var btnid = new Array('#mybuttonid1', '#mybuttonid2', 'mybuttonid3', 'mybuttonid4');
// Instantiates the variable that holds the media library frame.
var meta_image_frame;
// Runs when the image button ID clicked.
$.each(btnid).click(function(e) {
// Prevents the default action from occuring.
e.preventDefault();
// If the frame already exists, re-open it.
if (meta_image_frame) {
meta_image_frame.open();
return;
}
// Sets up the media library frame
meta_image_frame = wp.media.frames.meta_image_frame = wp.media({
title: meta_image.title,
button: { text: meta_image.button },
library: { type: 'image' }
});
// Runs when an image is selected.
meta_image_frame.on('select', function(){
if (btnid == ‘#mybuttonid1') {
var txtid = '#mytextid1’;
} else if (btnid == '#mybuttonid2') {
var txtid = '#mytextid2’;
} else if (btnid == '#mybuttonid3') {
var txtid = '#mytextid3’;
} else {
var txtid = '#htxmltiimgupdtlw';
}
// Grabs the attachment selection and creates a JSON representation of the model.
var media_attachment = meta_image_frame.state().get('selection').first().toJSON();
// Sends the attachment URL to our custom image input field.
$(txtid).val(media_attachment.url);
});
// Opens the media library frame.
meta_image_frame.open();
});
});
通知的靠不住的语法高亮?你有一个时髦的引用字符。 。 。 :-)。 – mgilson 2014-10-07 04:43:51
是@mgilson我注意到了,谢谢。但这不是问题,只是来自我的文本编辑器的复制和粘贴。这些奇怪的字符不在我的PHP文件。我希望问题很简单。但再次感谢让我知道,这是我第一次使用该网站。 – talkingcode 2014-10-07 18:15:50