我的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");
});
Run Code Online (Sandbox Code Playgroud)
其中,唯一感兴趣的是线条:
$.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);
}
});
Run Code Online (Sandbox Code Playgroud)
在使用语句时,this.prop("selected", true);我收到错误:
未捕获的TypeError:this.prop不是函数
当.prop()显然是一个存在的函数时,为什么会发生这种情况?我如何解决它?
$.each用于迭代对象或数组.如果要迭代jQuery对象中的节点,请使用.each如下所示:
$("#price_based_on").children('option').each(function() {
... code here
});
Run Code Online (Sandbox Code Playgroud)
在回调内部,this引用本机DOM元素(没有prop方法,所以你可能想要做这样的事情来获取对jQuery持有DOM节点的对象的引用:
$("#price_based_on").children('option').each(function() {
var $this = $(this);
$this.prop('selected',true)
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7889 次 |
| 最近记录: |