jQuery未捕获ReferenceError:HTMLButtonElement.onclick(index.html:1)上未定义i4

时间:2018-08-26 11:10:12

标签: javascript jquery

我正在使用localstorage来添加购物车,在这里我试图显示购物车项目并使用键删除。当我尝试通过在jquery中传递参数来删除项目时。 它在控制台中显示此错误

@RunWith( SpringJUnit4ClassRunner.class )
        public class WorkerTest {

            Worker worker=new Worker();

            @Test
            public void getAll() {
                List<Workers> workersList = worker.getAll();
            }

                @Test
                public void getById() {
                    Workers workers = worker.getById(7);
                }
            }

这是我在jquery中用于显示购物车项目的弹出函数,并且错误在于按钮onclick函数传递参数,即试图传递键入:

    index.html:1 Uncaught ReferenceError: i4 is not defined
        at HTMLButtonElement.onclick
    removeitem(1,i4);//i4 is not defined, it is key in localstorage.

我将此removeitem函数保留在jquery的范围之外,但保留在脚本标记之内

onclick ='removeitem("+i+','+keyitem+")'   



    $("#cartPOP").click(function(){
              var count = 1;
              var i =1;
              var k = 0;
              var keyitem = key;
              var qunatTotal = 0;
              var total = 0;
              Object.keys(localStorage).forEach(function(key){

              var item = "delitem"+i;
              var ffprice = "ffprice"+i;
              var fpvalueid = "fpvalueid"+i;
              var selectid = "selectid"+i;
              var trcount = "trcount"+i;
              keyitem = String(key);
              console.log("items...."+localStorage.getItem(key));
              var myobj =  JSON.parse(localStorage.getItem(key));
              console.log("keyyy...."+key);
              $("#tab1").append("<tr id = '"+trcount+"'>
                <td>"+count+"</td><td   id='fname'>"+myobj.food_name+"</td>
                <td>"+myobj.quantity+"</td>  
               <input value="+myobj.food_price+" id='"+fpvalueid+"'     type='hidden' />
               <td id="+ffprice+">Rs "+myobj.food_price+"<br>
               <small style='color: red;'> Included 5% gst and packing  charge</small>
                </td>
                <td>
               <button onclick ='removeitem("+i+','+keyitem+")'  id="+item+" class='fa fa-remove' style='font-size:20px;color:red'>delItem</button>
              </td>
              </tr>");
               count++;
               i++;
               k++;
              });
        });

我有这些问题:

如何在上面的代码中传递localstorage密钥以删除项目? 当在按钮事件的jquery append函数中使用此代码时,为什么会出现上述错误?但是,当我删除keyitem并在removeitem函数中仅传递一个参数function removeitem(x, key) { var id = "trcount"+x; console.log("remove_test>>> "+id); console.log("key>>> "+key); localStorage.removeItem(key);//here i am trying to remove cart using key var tt = document.getElementById(id); document.getElementById('tab1').removeChild(tt); } 时,这没什么错误,但是我的删除购物车功能无法正常工作。

i

请建议我,我尝试了很多方法但没有起作用。

0 个答案:

没有答案