Я протестировал ваш код, как есть, и на самом деле не получил сообщение «Uncaught TypeError: Can not read property» toLowerCase «неопределенной» ошибки через консоль. Однако мне удалось вызвать ошибку с помощью метода 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 демо выше кода.
В коде, который вы отправили, нет вызова 'toLowerCase'. – Pointy
Это потому, что ошибка возникает из кода jQuery, который применяет '.toLowerCase()' к переменной, которая, по-видимому, не определена. – Bobort