CoffeeScript и контекст - PullRequest
0 голосов
/ 06 июня 2018

Я пытаюсь получить значение $(this) с контекстом, в котором событие было инициировано, но я получаю следующую ошибку:

Uncaught TypeError: Невозможно прочитать свойство 'toLowerCase'неопределенных

cants_field_hidden = document.createElement('input')
cants_field_hidden.setAttribute 'type', 'hidden'
cants_field_hidden.setAttribute 'id', 'cant_' + if id == undefined then item_id else id
cants_field_hidden.classList.add 'cant_input'
cants_field_div.append cants_field_hidden

cants_field_visible = document.createElement('input')
cants_field_visible.setAttribute 'type', 'number'
cants_field_visible.setAttribute 'placeholder', 'Porciones'
cants_field_visible.setAttribute 'name', 'ammounts[]'
cants_field_visible.setAttribute "onkeypress", calculate_carbohydrates((if id == undefined then item_id else id), (if 
carbohydrates == undefined then item_carbohydrates else carbohydrates))
cants_field_visible.setAttribute 'required', true
cants_field_div.append cants_field_visible

function calculate_carbohydrates(id, carbohydrates){
  console.log($(this))
  console.log(id)
  console.log(carbohydrates)

  var input = document.getElementById('cant_' + id)
  var total_carbohydrates = $(this).val() * carbohydrates

  input.value = parseInt(total_carbohydrates)

  var sum = 0;
  $(".cant_input").each(function(){
    sum += Number($(this).val())
  })
  $("#total_carbohydrates").val(sum).trigger('change')
  $("#total_carbohydrates_span").text($("#total_carbohydrates").val())
}

1 Ответ

0 голосов
/ 07 июня 2018

Найти решение

cants_field_visible.addEventListener "keyup", ->
    calculate_carbohydrates.call(this, "#{if id == undefined then item_id else id}", "#{if carbohydrates == undefined then item_carbohydrates else carbohydrates}")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...