2016-12-18 4 views
0

私のjQueryのコードは次のとおりです。私はエラーを取得する声明this.prop("selected", true);を使用して上this.propは関数ではありませんか?

$.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); 
    } 
}); 

:このうち、

$('#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"); 
}); 

、興味のある唯一のものは線です

Uncaught TypeError: this.prop is not a function

.prop()が明らかに存在する関数である場合、なぜこれが起こりますか?どうすれば修正できますか?

+0

'this'をjQueryオブジェクトにする必要があります。' $(this).prop'は動作するはずです – TryingToImprove

答えて

6

$.eachは、オブジェクトまたは配列を反復するために使用されます。あなたは、jQueryオブジェクト内のノードを反復処理する。このよう.eachを使用したい場合は、次のコールバックインサイド

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

thispropメソッドを持っていないネイティブDOM要素(を意味し、そうおそらくDOMノードを保持しているjQueryオブジェクトへの参照を取得するには、このような何かをしたい:

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

thisはjQueryオブジェクトではありません$(this)

を使用してください。
$(this).prop("selected", true); 
4

thisはjQueryオブジェクトではありません、あなたはそれjqueryのオブジェクトにするために周りのjqueryのセレクタ$()を追加し、そう$(this)にそれを変更する必要があります。

関連する問題