首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CoffeeScript和上下文

CoffeeScript和上下文
EN

Stack Overflow用户
提问于 2018-06-07 02:11:47
回答 1查看 81关注 0票数 0

我试图通过触发事件的上下文来获取值$(this),但是我得到了以下错误:

未捕获TypeError:无法读取未定义的属性“”toLowerCase“”

代码语言:javascript
复制
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())
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50726963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档