2014-11-28 10 views
1
$('#sum').keydown(function(){ 
      updateResultPrice(); 
     }); 

     function updateResultPrice() { 
      ajax('/payment/summ', 'price='+$(this).val()); 
     } 

Not working! 콘솔 로그 인쇄 : catch되지 않은 형식 오류 : 당신은 .toLowerCase()에 대한 호출이없는 정의되지 않은잡히지 않은 유형 오류 : 정의되지 않은 'toLowerCase'속성을 읽을 수 없습니다.

+2

게시 한 코드에'toLowerCase' 호출이 없습니다. – Pointy

+0

분명히 정의되지 않은 변수에 .toLowerCase()를 적용하는 jQuery 코드에서 오류가 발생하기 때문입니다. – Bobort

답변

5

의 재산 '와 toLowerCase'을 읽을 수 없습니다,하지만 난 당신이 .val()의 말미에 체인 것 같은데요.

this 값이 window이고 #sum 요소가 아닙니다.

이 당신의 핸들러를 변경

: 핸들러가 호출 될 때

$('#sum').keydown(updateResultPrice); // <-- pass the function directly 

function updateResultPrice() { 
    ajax('/payment/summ', 'price='+$(this).val().toLowerCase()); 
} 

지금, this#sum 변수를 참조하고 .val()undefined를 반환하지 않습니다.

0

코드를 테스트 한 결과 콘솔에서 "Uncaught TypeError : 속성을 'Undefined"의 LowCase'오류로 읽을 수 없습니다. "오류가 발생했습니다. 그러나 ajax() 메서드로 오류를 트리거 할 수있었습니다.

코드가 작동하지 않는 이유는 $(this)이라는 사실은 window과 같으며 #sum 요소가 아닙니다. six fingered man 그의 답변에서 이것을 설명했다.

대신이 코드를 사용해보십시오. 당신의 시험 즐거움을 위해

// Switch 'keydown' to 'on' and include 'keyup' event to get the actual data; 
// The 'on' method allows you to "string" events together. 'keyup keydown click focus...' etc. 
$('#sum').on('keyup', function(){ 
    // Define a variable to make calling 'this' easier to write; 
    var me = $(this); 
    // Get the value of "me"; 
    var val = me.val(); 

    // Relay the value to the function; 
    updateResultPrice(val); 
}); 

// The function, updateResultPrice, accepts an argument of "value"; 
function updateResultPrice(value) { 
    // Your prior code used $(this).val() within the function; 
    // The function doesn't have a $(this) to retreive the value from it, 
    // So, use the "value" argument; 
    $.ajax('/payment/summ', 'price=' + value); // Call "$.ajax", not "ajax"; 
    // The above snippet will trigger a 404, should the file not exist. 

    // Just to test that it works, log it to the console; 
    console.log('the price is: '+value); 
} 

, 여기에 위의 코드의 JSFiddle 데모입니다.

관련 문제