Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript修复onclick函数不起作用_Javascript_Jquery_Html_Json - Fatal编程技术网

如何使用JavaScript修复onclick函数不起作用

如何使用JavaScript修复onclick函数不起作用,javascript,jquery,html,json,Javascript,Jquery,Html,Json,有人能帮我吗?这是我的问题。当我试图使用此函数单击按钮(“.btn_cellophane”)。打开(“单击”,函数(){})我没有得到任何响应,甚至没有收到错误。我猜onclick功能不起作用。下面是我的代码 方法调用 $(".btn_cellophane").on("click",function(){ alert("Tester true"); if($(this).hasClass("btn-warning")){ $(this).r

有人能帮我吗?这是我的问题。当我试图使用此函数单击
按钮
(“.btn_cellophane”)。打开(“单击”,函数(){})我没有得到任何响应,甚至没有收到错误。我猜
onclick
功能不起作用。下面是我的代码

方法调用

$(".btn_cellophane").on("click",function(){
     alert("Tester true");
         if($(this).hasClass("btn-warning")){
              $(this).removeClass("btn-warning");
              $(this).addClass("btn-success");

              // add_amount_for_cellophane();

         }else if($(this).hasClass("btn-success")){
              $(this).addClass("btn-warning");
              $(this).removeClass("btn-success");

              // add_amount_for_cellophane();
         }
});
内容显示

var html_cellophane = "";
$.each(data, function(key,product){

        $.each(product.sizes, function(pro_id,variant){

             if(variant.prod_type == 1){
                   var label = (variant.size+" "+variant.unit).toUpperCase();
                   var var_id = variant.variant_id;
                   var price = variant.price;

                       html_cellophane += '<div>'+
                                                '<label>Cellophane ('+price+' pesos)</label>'+
                                           '</div>'+
                                           '<br/>'+
                                           '<div style="width: 100%;">'+
                                                 '<button type="button" class="btn btn-warning btn_cellophane" id="cp_id_btn" data-var_id="'+var_id+'" data-qty="'+1+'" data-price="'+price+'">1</button>'+
                                                 '<button type="button" class="btn btn-warning btn_cellophane" id="cp_id_btn" data-var_id="'+var_id+'" data-qty="'+2+'" data-price="'+price+'">2</button>'+
                                                 '<button type="button" class="btn btn-warning btn_cellophane" id="cp_id_btn" data-var_id="'+var_id+'" data-qty="'+3+'" data-price="'+price+'">3</button>'+
                                                 '<button type="button" class="btn btn-warning btn_cellophane" id="cp_id_btn" data-var_id="'+var_id+'" data-qty="'+4+'" data-price="'+price+'">4</button>'+
                                                 '<button type="button" class="btn btn-warning btn_cellophane" id="cp_id_btn" data-var_id="'+var_id+'" data-qty="'+5+'" data-price="'+price+'">5</button>'+
                                                  '<hr>'+                                        
                                           '</div>';

              }                                     

         });                                                 


});                  

$('.btn_cellophane_div ').html(html_cellophane);
var html_cellophane=”“;
$。每个(数据、功能(键、产品){
$。每个(产品尺寸、功能(pro_id、变体){
if(variant.prod_type==1){
变量标签=(variant.size+“”+variant.unit).toUpperCase();
var var_id=variant.variant_id;
var价格=变量价格;
html_玻璃纸+=“”+
'玻璃纸('+价格+'比索)'+
''+
“
”+ ''+ '1'+ '2'+ '3'+ '4'+ '5'+ “
”+ ''; } }); }); $('.btn_cellophane_div').html(html_cellophane);
您已经动态创建了按钮。因此,当您附加事件侦听器时,元素在此之前不存在。因此,您必须使用来处理单击事件

只需像这样更改事件侦听器-

$(document).on("click",".btn_cellophane", function(){});

.btn_玻璃纸
元素在附加事件时不存在。jQuery总是返回一个对象,这就是为什么不会触发错误。这可能会对您有所帮助。您是否已将单击处理程序放入$(document).ready()中?