jQuery $这个点击元素

时间:2012-10-06 04:09:10

标签: javascript jquery

我有以下脚本在点击时从一个模态窗口向输入字段发送图像URL。我也将同一个网址imgurl发送到<div class="preview-image>,以便在插入时显示。

var BusiPress_Configuration = {
    init : function() {
        this.files();
    },

    files : function() {
        if ( $( '.busipress_upload_image_button' ).length > 0 ) {
            window.formfield = '';

            $('.busipress_upload_image_button').on('click', function(e) {
                e.preventDefault();
                window.formfield = $(this).parent().prev();
                window.tbframe_interval = setInterval(function() {
                    jQuery('#TB_iframeContent').contents().find('.savesend .button').val(busipress_vars.use_this_file).end().find('#insert-gallery, .wp-post-thumbnail').hide();
                }, 2000);
            if (busipress_vars.post_id != null ) {
                var post_id = 'post_id=' + busipress_vars.post_id + '&';
            }
                tb_show(busipress_vars.add_new_file, 'media-upload.php?' + post_id +'TB_iframe=true');
            });

            window.original_send_to_editor = window.send_to_editor;
            window.send_to_editor = function (html) {
                if (window.formfield) {
                    imgurl = $('a', '<div>' + html + '</div>').attr('href');
                    window.formfield.val(imgurl);
                    window.clearInterval(window.tbframe_interval);
                    tb_remove();
                    $('.preview-image img').attr('src',imgurl);
                } else {
                    window.original_send_to_editor(html);
                }
                window.formfield = '';
                window.imagefield = false;
            }
        }
    }
}

BusiPress_Configuration.init();

将图像URL发送到编辑器并且div工作正常,但它正在为页面上该div的每个实例执行此操作。我一直在玩$(this)closest()以查看我是否可以将输入本地化到输入附近的特定div,但没有任何运气。任何帮助将不胜感激。谢谢!

默认标记

  <table class="form-table">
    <tbody>
      <tr valign="top">
        <th scope="row">Default Map Icon</th>

        <td>
          <input type="text" class="-text busipress_upload_field" id=
          "busipress_settings_map[default_map_icon]" name=
          "busipress_settings_map[default_map_icon]" value=
          "http://localhost/jhtwp/wp-content/plugins/busipress/img/red-dot.png" /><span>&nbsp;<input type="button"
          class="busipress_upload_image_button button-secondary" value=
          "Upload File" /></span> <label for=
          "busipress_settings_map[default_map_icon]">Choose the default map icon (if
          individual business type icon is not set</label>

          <div class="preview-image" style="padding-top:10px;"><img src=
          "http://localhost/jhtwp/wp-content/plugins/busipress/img/red-dot.png" /></div>
        </td>
      </tr>

      <tr valign="top">
        <th scope="row">Active Map Icon</th>

        <td>
          <input type="text" class="-text busipress_upload_field" id=
          "busipress_settings_map[active_map_icon]" name=
          "busipress_settings_map[active_map_icon]" value=
          "http://localhost/jhtwp/wp-content/plugins/busipress/img/blue-dot.png" /><span>&nbsp;<input type="button"
          class="busipress_upload_image_button button-secondary" value=
          "Upload File" /></span> <label for=
          "busipress_settings_map[active_map_icon]">Choose the active map icon (if
          individual business type icon is not set</label>

          <div class="preview-image" style="padding-top:10px;"><img src=
          "http://localhost/jhtwp/wp-content/plugins/busipress/img/blue-dot.png" /></div>
        </td>
      </tr>
    </tbody>
  </table>

1 个答案:

答案 0 :(得分:0)

假设您已经可以选择按钮(我在此期间将其更改为“按钮选择器”),请使用parent获取父td,然后找到孩子div改变了。这将获得距您的按钮最近的div

$("button-selector").parent('td').find("div")