最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - this.prop is not a function? - Stack Overflow

programmeradmin16浏览0评论

My jQuery code is:

$('#edit').click(function(){
    var data = $("#list :input").serialize();
    $.post($("#list").attr('action'), data, function(json) 
    {
        currentRow = json.rowArr[0];
        $("#id").val(currentRow.id);
        $("#id_disp").val(currentRow.id);
        $("#shop").val(currentRow.shop);
        $("#category").val(currentRow.category);
        $("#item").val(currentRow.item);
        $("#qnty").val(currentRow.qnty);
        $("#unit").val(currentRow.unit);

        $.each($("#price_based_on").children('option'), function(index, val) {
            if(this.value.toUpperCase()==currentRow.price_based_on.toUpperCase())
            {
                console.log("Match");
                this.prop("selected", true);
            }
        });

        $("#mrp").val(currentRow.mrp);
        $("#sellers_price").val(currentRow.sellers_price);
        $("#last_updated_on").val(currentRow.last_updated_on);

    },"json");
});

Among this, the only thing of interest are the lines:

$.each($("#price_based_on").children('option'), function(index, val) {
    if(this.value.toUpperCase()==currentRow.price_based_on.toUpperCase())
    {
        console.log("Match");
        this.prop("selected", true);
    }
});

On using the statement this.prop("selected", true); I get the error:

Uncaught TypeError: this.prop is not a function

Why does this happen when .prop() is clearly a function that exists? How do I fix it?

My jQuery code is:

$('#edit').click(function(){
    var data = $("#list :input").serialize();
    $.post($("#list").attr('action'), data, function(json) 
    {
        currentRow = json.rowArr[0];
        $("#id").val(currentRow.id);
        $("#id_disp").val(currentRow.id);
        $("#shop").val(currentRow.shop);
        $("#category").val(currentRow.category);
        $("#item").val(currentRow.item);
        $("#qnty").val(currentRow.qnty);
        $("#unit").val(currentRow.unit);

        $.each($("#price_based_on").children('option'), function(index, val) {
            if(this.value.toUpperCase()==currentRow.price_based_on.toUpperCase())
            {
                console.log("Match");
                this.prop("selected", true);
            }
        });

        $("#mrp").val(currentRow.mrp);
        $("#sellers_price").val(currentRow.sellers_price);
        $("#last_updated_on").val(currentRow.last_updated_on);

    },"json");
});

Among this, the only thing of interest are the lines:

$.each($("#price_based_on").children('option'), function(index, val) {
    if(this.value.toUpperCase()==currentRow.price_based_on.toUpperCase())
    {
        console.log("Match");
        this.prop("selected", true);
    }
});

On using the statement this.prop("selected", true); I get the error:

Uncaught TypeError: this.prop is not a function

Why does this happen when .prop() is clearly a function that exists? How do I fix it?

Share Improve this question asked Dec 18, 2016 at 18:36 Somenath SinhaSomenath Sinha 1,2143 gold badges19 silver badges37 bronze badges 1
  • You need to make this to a jQuery object.. $(this).prop should work – TryingToImprove Commented Dec 18, 2016 at 18:37
Add a comment  | 

3 Answers 3

Reset to default 10

$.each is used to iterate over an object or array. If you want to iterate over the nodes in a jQuery object, use .each like this:

$("#price_based_on").children('option').each(function() {
     ... code here
});

Inside the call back, this refers to the native DOM element (which doesn't have a prop method, so you probably want to do something like this to get a reference to the jQuery object that holds the DOM node:

$("#price_based_on").children('option').each(function() {
    var $this = $(this);
    $this.prop('selected',true)
});

this is not a jquery object, you need to add jquery selector $() around to make it jquery object, so change it to $(this).

this is not a jQuery object. Use $(this)

$(this).prop("selected", true);
发布评论

评论列表(0)

  1. 暂无评论