0

我已经动态加载了带有按钮的字段,例如

<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]现在我想为所有人修改这个 jquery 函数products_image_large_new_[any-number]

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]什么?

4

2 回答 2

1

id要匹配以s 开头的所有元素#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');
于 2012-10-09T03:26:31.407 回答
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;

      });
    });
于 2012-10-09T03:27:26.037 回答