简体   繁体   English

jQuery不适用于Firefox,但适用于Chrome

[英]jQuery doesn't work on Firefox but works on Chrome

I have this jQuery code: 我有这个jQuery代码:

function action(action){
    event.preventDefault();
    var products = $("#check-list input:checkbox:checked").map(function(){
      return $(this).val();
    }).get();
    var string = JSON.stringify(products);
    var table = $('#table').val();

    if(products.length != '0' || action === 'old-all'){
        $.ajax({
            type: 'post',
            url: 'app/toolbar.php',
            data: {action:action, table:table, ids:string},
            success:function(data){
                localStorage.setItem('notify', data);
                location.reload();
            },
        });
    } else{
        notify('You must choose an element first');
    }
}

I'm using jQuery 3.3.1 . 我正在使用jQuery 3.3.1

If i click a button without checking a checkbox i should get You must choose an element first but if i choose an element the toolbar.php should run and do the thing it has to be done(according to which button was pressed: 如果我单击一个按钮而没有选中复选框我应该得到You must choose an element first但如果我选择一个元素, toolbar.php应该运行并做必须完成的事情(根据按下的按钮:

<button class="grey-btn" onClick="action('delete')">Delete</button>
<button class="grey-btn" onClick="action('update')">Update</button>

It works on Chrome perfectly but it doesn't work on Firefox . 它完全适用于Chrome ,但它不适用于Firefox I was trying some answers that i found on some other similar 'works on chrome but not on firefox' questions but neither one of them worked :( 我正在尝试一些答案,我在其他类似的'铬上的作品,但没有在Firefox上'找到答案,但他们都没有工作:(

You do not define event so that is your problem. 您没有定义事件,因此这是您的问题。 Get away from using inline event handlers and bind it with jQuery. 远离使用内联事件处理程序并将其与jQuery绑定。

 $("[data-action]").on("click", function (event) { event.preventDefault(); var btn = $(this); var action = btn.data("action"); console.log(action); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="grey-btn" data-action='delete'>Delete</button> <button class="grey-btn" data-action='update'>Update</button> 

If you really want to use inline event handlers than do 如果你真的想使用内联事件处理程序而不是

 function action (event, method) { event.preventDefault(); console.log(method); } 
 <button class="grey-btn" onClick="action(event, 'delete')">Delete</button> <button class="grey-btn" onClick="action(event, 'update')">Update</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery模糊在Firefox和Chrome中不起作用,但在IE9中起作用 - Jquery blur doesn't work in Firefox and Chrome but works in IE9 Jquery / Javascript和Css条件在firefox和IE上不起作用。 它适用于Chrome - Jquery/Javascript and Css condition doesn't work on firefox and IE. It works on Chrome 网页无法在Firefox中使用,但可以在Chrome和Torch中使用 - Webpage doesn't work in Firefox but works in Chrome and Torch Cytoscape.js-layout()在Firefox中无效,在Chrome中有效 - Cytoscape.js - layout() doesn't work in Firefox, works in Chrome .unbind(&#39;submit&#39;)。submit()在Chrome / IE中有效,但在Firefox中无效 - .unbind('submit').submit() works in Chrome/IE but doesn't work in Firefox JS正则表达式代码不适用于Firefox,但适用于Chrome和IE - JS regex code doesn't work with firefox, but works on chrome and IE 该Javascript在IE中不起作用,但在Chrome,Firefox和Opera中起作用 - This Javascript doesn't work in IE but works in Chrome, Firefox and Opera 提交按钮不适用于Chrome或Firefox,但适用于Internet Explorer - The Submit Button doesn't work in Chrome or Firefox, but Works in Internet Explorer 页面在 Firefox 中无法正常工作,但在 chrome 中工作正常 - Page doesn't work as intended in Firefox but works fine in chrome Javascript不适用于Chrome和Firefox,但适用于IE - Javascript doesn't work on Chrome and Firefox, but works on IE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM