按钮onclick无效。它显示为功能未定义。我需要将按钮的id传递给deleteItem函数以删除该行

时间:2016-03-02 04:49:07

标签: coffeescript

如何在点击coffeescript.deleteItem时将按钮的id传递给删除功能是删除功能。按钮onclick不起作用。它显示为功能未定义。我需要将按钮的id传递给deleteItem函数,以便删除行

 # out: ./bill.js
    itemArray = []
    costArray = []
    results = ""
    maxId = 1
    getData = () ->
      item = document.getElementById("item").value
      cost = document.getElementById("cost").value
      quantity = document.getElementById("quantity").value
      object = {id:maxId,item:item,cost:cost*quantity,quantity:quantity}
      itemArray.push object
      maxId = maxId + 1
      display()

    deleteItem = () ->
      console.log "delete"

    display = () ->
      subTotal = 0
      tax = 0
      total = 0
      for item in itemArray
        subTotal = subTotal + item.cost
      tax = ((subTotal/100)*15)
      total = subTotal + tax
      j = 0
      results = "<table>"
      results+="<tr><td>"+"</td>"+"<td>"+"Item"+"</td>"+"<td>"+"Quantity&nbsp&nbsp"+"</td>"+"<td>"+"Price"+"</td></tr>"
      for item in itemArray
        j = j + 1
        results += "<tr><td>"+ "&nbsp&nbsp&nbsp&nbsp&nbsp" + j + "&nbsp&nbsp&nbsp&nbsp&nbsp"+ "</td>"
        results += "<td>"+ item.item+ "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" + "</td>"
        results += "<td>"+ item.quantity + "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +"</td>"
        results += "<td>#{item.cost}</td><td><button id='#{item.id}' onclick = deleteItem /></td</tr>"
      results+="<tr><td>"+"</td>"+"<td>"+"</td>"+"<td>"+"Sub Total"+"</td>"+"<td>"+subTotal+"</td></tr>"
      results+="<tr><td>"+"</td>"+"<td>"+"</td>"+"<td>"+"Tax"+"</td>"+"<td>"+tax+"</td></tr>"
      results+="<tr><td>"+"</td>"+"<td>"+"</td>"+"<td>"+"Total"+"</td>"+"<td>"+total+"</td></tr>"
      billDisplay = document.getElementById("billDisplay")
      billDisplay.className = "rows"
      billDisplay.innerHTML = results


    addButton = document.getElementById("addButton")
    addButton.onclick = getData

1 个答案:

答案 0 :(得分:0)

在这种情况下,您可以为方法提供元素(this)或直接提供id

<button id="someId" onclick="myFunction(this,  'someId')">...

或在你的情况下

<button id='#{item.id}' onclick =\"deleteItem(this,'#{item.id}')\" />