单击每个动态加载按钮的功能

时间:2012-10-09 03:22:29

标签: jquery

我使用

等按钮动态加载字段
<input class="upload_image_23" type="text" size="36" name="products_image_large_new_23" value="" />
<input class="upload_image_button_24" type="button" value="Upload Image" />
<input class="upload_image_23" type="text" size="36" name="products_image_large_new_23" value="" />
<input class="upload_image_button_24" type="button" value="Upload Image" />

现在我想为所有upload_image_button_[any-number]products_image_large_new_[any-number]修改此jquery函数

jQuery(document).ready(function() {

jQuery('.upload_image_button_[any-number]').click(function() {
 formfield = jQuery('.upload_image_[any-number]').attr('name');
 tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
 return false;
});

window.send_to_editor = function(html) {
 imgurl = jQuery('img',html).attr('src');
 jQuery('.upload_image_[any-number]').val(imgurl);
 tb_remove();
}

});

我应该为[any-number]包含哪些内容?

2 个答案:

答案 0 :(得分:1)

要匹配所有以id开头的#upload_image_元素,请使用此选择器:

input[id^="upload_image_"]

您的代码将成为:

jQuery('input[class^="upload_image_button_"]').click(function() {
  var id = jQuery(this).attr('class').split('_').slice(-1);
  formfield = jQuery('.upload_image_' + id).attr('name');

  tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
  return false;
});

在此处阅读有关属性选择器的更多信息:http://api.jquery.com/category/selectors/attribute-selectors/

如果您的元素符合完全顺序,您也可以使用它来获取formfield

  formfield = jQuery(this).prev().attr('name');

答案 1 :(得分:0)

 $(document.body).click(function () {
      $("div").each(function (i) {
       formfield = jQuery('#upload_image_[any-number]').attr('name');
      tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
     return false;

      });
    });