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