Javascript 在jquery中使用带有click函数的foreach是否语法正确?
我正在尝试创建一个数组以使用多个唯一ID。我想我需要使用foreach或者可能只是每个语句来让代码工作,但它仍然不能。两种我都试过了。是我的语法错误,还是我使用的方法错误。if语句使用单个var对象工作正常。我希望我能清楚地说明这一点,我是jQuery新手。我还应该提到,这是一堆Wordpress自定义元框字段。此外,如果BTNID和TXTID是单对象变量,这也可以很好地工作Javascript 在jquery中使用带有click函数的foreach是否语法正确?,javascript,jquery,wordpress,Javascript,Jquery,Wordpress,我正在尝试创建一个数组以使用多个唯一ID。我想我需要使用foreach或者可能只是每个语句来让代码工作,但它仍然不能。两种我都试过了。是我的语法错误,还是我使用的方法错误。if语句使用单个var对象工作正常。我希望我能清楚地说明这一点,我是jQuery新手。我还应该提到,这是一堆Wordpress自定义元框字段。此外,如果BTNID和TXTID是单对象变量,这也可以很好地工作 jQuery(document).ready(function($){ var btnid = new Arr
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();
});
});
试试这个:
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, function(){
$(this).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我注意到了,谢谢。但这不是问题所在,这只是从我的文本编辑器复制和粘贴的结果。这些奇怪的字符不在我的php文件中。我希望问题就这么简单。但再次感谢你让我知道,这是我第一次使用这个网站。谢谢你阿明,但它仍然不起作用。我还尝试了这些分支,只打开代码示例,没有运气:$。每个btnid,函数{$this.value.clickfunctione{$。每个btnid,函数值{$this.value.clickfunctione{还尝试了更改数组的设置方式:var btnid=['mybuttonid1','mybuttonid2','mybuttonid3','mybuttonid4'];button函数没有打开meta_image_frame DOM。同样,当我使用单个变量作为元素时,效果很好。我正在考虑采用稍微不同的方法,因为。每个方法都不起作用。如果我将按钮选择器设为常规ID,那么任何具有特定.class的按钮都将是选择器,然后提取attrID,然后再提取if,会怎么样语句起作用,例如:var btnid=$':button.myClass.attr'id';按钮需要有一个.class,否则页面上的每个按钮输入都会打开WP图像上传程序。我知道我的语法仍然错误,但这是可能的。如果是,它实际上是如何编码的?再次感谢!