Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 这个.prop不是函数吗?_Javascript_Jquery - Fatal编程技术网

Javascript 这个.prop不是函数吗?

Javascript 这个.prop不是函数吗?,javascript,jquery,Javascript,Jquery,我的jQuery代码是: $('#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").

我的jQuery代码是:

$('#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");
});
其中,唯一有趣的是以下几行:

$.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);
    }
});
使用语句
this.prop(“selected”,true)我得到错误:

未捕获类型错误:this.prop不是函数


当.prop()显然是一个存在的函数时,为什么会发生这种情况?如何修复它?

$。每个
用于迭代对象或数组。如果要迭代
jQuery
对象中的节点,请使用
。每个
如下所示:

$("#price_based_on").children('option').each(function() {
     ... code here
});
在回调中,
this
引用本机
DOM
元素(它没有
prop
方法,因此您可能希望执行类似操作以获取对保存
DOM
节点的
jQuery
对象的引用:

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

不是jQuery对象。请使用
$(此)


不是jquery对象,您需要在周围添加jquery选择器
$()
使其成为jquery对象,因此将其更改为
$(this)

您需要将
设置为jquery对象。
$(this)。prop
应该可以工作
$(this).prop("selected", true);