$(this)没有为这个javascript自动完成功能提供dom元素

时间:2016-07-26 17:48:17

标签: javascript jquery

我需要元素的id / class ...但在第一种情况下它不会来。 请告诉我如何获取当前元素id / class。 $(这个)在这里不起作用。 在第一种情况下,row_num值未定义,而在第二种情况下,可以获得值。 请帮忙。

    (function() {        
    $(".searchProduct").autocomplete({source: function( request, response ){
             var row_num = $(this).attr('data-rownum'); // need the value here
             var product_ids =  $(".thisweekwt_"+row_num).val();
            console.log(row_num); // value is undefined
            $.ajax({url: "<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'search_auto')); ?>",
                type: "POST",
                dataType: "json",
                data: {q: request.term, product_ids},
                success: function( data ) {
                    response(data);              
                }
            });
        },
        minLength: 2,
        select: function(event, ui) {
            var id = ui.item.id;
            var price = parseInt(ui.item.title);
            var weight = ui.item.id;
            var table_id = $(this).parent().parent().attr('id');
            var key_num = $(this).attr('data-keynum');
            var row_num = $(this).attr('data-rownum');
            var key = row_num+"_"+key_num;
            var text_box_id = $(this);
            var row = document.getElementById(table_id);
              $.ajax({
                url: "<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'month_box_push')); ?>",
                type: 'POST',
                data: {id:id, key:key, row_num:row_num},
                success: function (response) {
                    var num = parseInt(key_num) + 1;
                    text_box_id.attr("data-keynum", num); 
                    newRow = response;
                    $(row).before(newRow);
                    var this_box_price = adding_box_price(row_num);                        $("#box"+row_num).text("Rs."+this_box_price);console.log(row_num); // value is defined                        $('#thisweekid_'+row_num).val($('#thisweekid_'+row_num).val()+","+id);
                    var this_box_wts = adding_box_pro_wt(row_num);
                    $(".thisweekwt_"+row_num).val(this_box_wts);
                },
                error: function (response) {
                    console.log(response);
                }
            });
            return false;   
        }
    });
});

2 个答案:

答案 0 :(得分:0)

您应该使用ui.item

select: function(event, ui) {
   var $this = ui.item;
   // the rest of you adapted code
}

答案 1 :(得分:0)

通过迭代每个.searchProduct元素,给出一个摆动,你可以在初始化自动完成时提供的回调中专门引用它们。

(function() {        
    $(".searchProduct").each(function(){
        var $searchEl = $(this);
        $searchEl.autocomplete({
            source: function( request, response ){
                 var row_num = $searchEl.attr('data-rownum'); // need the value here
                 var product_ids =  $(".thisweekwt_"+row_num).val();
                console.log(row_num); // value is undefined
                $.ajax({url: "<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'search_auto')); ?>",
                    type: "POST",
                    dataType: "json",
                    data: {q: request.term, product_ids},
                    success: function( data ) {
                        response(data);              
                    }
                });
            },
            minLength: 2,
            select: function(event, ui) {
                var id = ui.item.id;
                var price = parseInt(ui.item.title);
                var weight = ui.item.id;
                var table_id = $searchEl.parent().parent().attr('id');
                var key_num = $searchEl.attr('data-keynum');
                var row_num = $searchEl.attr('data-rownum');
                var key = row_num+"_"+key_num;
                var row = document.getElementById(table_id);
                  $.ajax({
                    url: "<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'month_box_push')); ?>",
                    type: 'POST',
                    data: {id:id, key:key, row_num:row_num},
                    success: function (response) {
                        var num = parseInt(key_num) + 1;
                        $searchEl.attr("data-keynum", num); 
                        newRow = response;
                        $(row).before(newRow);
                        var this_box_price = adding_box_price(row_num);
                        $("#box"+row_num).text("Rs."+this_box_price);
                        console.log(row_num); // value is defined
                        $('#thisweekid_'+row_num).val($('#thisweekid_'+row_num).val()+","+id);
                        var this_box_wts = adding_box_pro_wt(row_num);
                        $(".thisweekwt_"+row_num).val(this_box_wts);
                    },
                    error: function (response) {
                        console.log(response);
                    }
                });
                return false;   
            }
        });
    })
});