0

我有一个脚本,在单击时执行 ajax 调用连接到数据库获取图像名称并在 < -img - > 内设置图像名称,并在其后添加一个隐藏的复选框,然后回显它。

然后我将返回的 ajax 消息作为 div 的 HTML。我的问题是我能否对插入的内容执行更多操作..

主要目标是能够像单击复选框一样单击图像(这部分已经为我排序)但是无论我尝试什么,我都无法使用 .click 功能..

这是代码。

这是与图像相呼应的 PHP 部分。

if($_POST['updateIgallery'] == 'ajax'){
    global $wpdb;
    $table_name= $wpdb->prefix . "table_T";
    $imagecounter = 1;
    $toecho = '';
    $currentselected = $wpdb->get_row("query");
    preg_match_all('/\/(.+?\..+?)\//',$currentselected ['image_gal'],$preresualts); // images are stored with /image/.
    foreach ($preresualts[1] as $imagename){
        $toecho .= '
        <img rel="no" id="JustantestID" class="JustaTestClass" src="'.site_url().'/wp-content/plugins/wp-ecommerce-extender/images/uploads/'.$imagename.'">
        <input name="DoorIMGtoDeleteIDcheck'.$imagecounter.'" style="display:none;" name="DoorIMGtoDelete['.$imagecounter.']" value="/'.$imagename.'/" type="checkbox">
        ';
        $imagecounter++;
    }
    echo $toecho;
}

这是发送和接收 HTML 并将 HTML 插入 div 的 ajax 部分:

$.ajax({
        type: "POST",
        url: "/wp-content/plugins/wp-ecommerce-extender/DB_Functions.php",
        data: { updateIgallery: "ajax", CurrentDoorIDnum: $('#dooridforgallery').val()}
        }).success(function(insertID) {
                $("#ImgGalleryID").html(insertID);
            });

到目前为止,我遇到的问题如下:

$("#JustantestID").click(function() {
//DoorImageGallery($(this).attr('id')); // the function i will use if the alert actually works
    alert("kahdaskjdj");
    return true;
 });

我希望问题和代码是可以理解的。

先谢谢了。

4

3 回答 3

3

当您替换元素的 html 时,其中的所有元素都会被删除并消失。这意味着附加到它们的事件处理程序也将被删除。

您可以尝试将事件处理程序附加到页面上静态且永久的更高级别元素。没有更多信息,我将使用document

$(document).on( "click", "#yaniv", function() {
    alert("kahdaskjdj");
});
于 2012-07-28T14:38:41.777 回答
0

由于元素是使用 ajax 动态插入到 DOM 中的,因此您必须将事件委托给绑定单击处理程序时实际存在的父元素,在这种情况下看起来是#ImgGalleryID

$('#ImgGalleryID').on('click', '#yaniv', function() {
    DoorImageGallery(this.id);
    alert("kahdaskjdj");
});
于 2012-07-28T14:39:24.853 回答
0
$('img.JustaTestClass').bind('click', function() {
  var checkbox = $(this).siblings('input[type=checkbox]');
  if (!checkbox.is(':checked')) checkbox.attr('checked', true);
  else checkbox.attr('checked', false);
});
于 2012-07-28T14:39:15.610 回答