2014-10-07 78 views
0

我想创建一个数组以便使用多个唯一的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(); 
    }); 
}); 
+1

通知的靠不住的语法高亮?你有一个时髦的引用字符。 。 。 :-)。 – mgilson 2014-10-07 04:43:51

+0

是@mgilson我注意到了,谢谢。但这不是问题,只是来自我的文本编辑器的复制和粘贴。这些奇怪的字符不在我的PHP文件。我希望问题很简单。但再次感谢让我知道,这是我第一次使用该网站。 – talkingcode 2014-10-07 18:15:50

回答

0

试试这个:

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(); 
    }); 
    }); 
}); 
+0

谢谢阿明,但它仍然不起作用。我也尝试了这些分支(开放代码只是例子),没有运气: '$ .each(btnid,function(){ $(this.value).click(function(e){ $ .each(btnid ,函数(value){(this.value).click(function(e){' 也尝试改变如何设置数组: 'var btnid = ['#mybuttonid1','#mybuttonid2',' mybuttonid3','mybuttonid4'];' 按钮函数没有打开meta_image_frame DOM,再次,当我使用单个var作为元素时,效果很好 – talkingcode 2014-10-07 17:58:14

+0

我在考虑采用稍微不同的方法。 each()不起作用。如果我使按钮选择器成为通用ID,那么使用a例如: 'var btnid = $(':button.myClass')。attr('id');' 按钮需要有一个.class,否则页面上的每个按钮输入都会打开WP图像上传器。我知道我的语法仍然是错误的,但这是可能的。如果是这样,它将如何编码?再次感谢! – talkingcode 2014-10-07 19:10:32