Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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 jQuery选择器工作不正常_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery选择器工作不正常

Javascript jQuery选择器工作不正常,javascript,jquery,html,Javascript,Jquery,Html,这是我的HTML: <div class="alert alert-info alert-warning" id="rowcounter" style="display: block; bottom: -22px;"> Are you sure you want to delete 103? <div class="btn-toolbar"> <a href="#" class="btn btn-sm btn-success" dat

这是我的HTML:

<div class="alert alert-info alert-warning" id="rowcounter" style="display: block; bottom: -22px;">
    Are you sure you want to delete 103?
    <div class="btn-toolbar">
        <a href="#" class="btn btn-sm btn-success" data-action="deleterow" data-table="contact" data-rowid="103">Yes</a>
        <a href="#" class="btn btn-sm btn-default" data-action="cancel">No</a>
    </div>
</div>
不管出于什么原因,jQuery没有选择这个工具栏中的按钮,我不知道为什么。如果有人能给我一个原因,为什么这是不工作,将不胜感激

试试这个:

 $('#rowcounter .btn-toolbar a')

使用rowcounter a.btn而不是.alertrowcounter a.btn

$('#rowcounter a.btn').on('click',function() {
    console.log('yes');
    if($(this).data('action') == 'cancel') {
        rowCounter('hide');
    }
    if($(this).data('action') == 'deleterow') {
        var tbl = $(this).data('table');
        var targ = $(this).data('rowid');
        $.get('formhandler.php?handle=deleterow&table='+tbl+'&rowid='+targ,function(d) {
            if(d == 'success') {
                $('#rowcounter').removeClass().addClass('alert alert-success').html('Successfully removed '+targ+'!');
            }
        });
    }
});

我纯粹是猜测,但选择一个类然后id似乎很奇怪。使用rowcounter.alter a.btn是否会得到不同的结果??是否在单击时在控制台中记录“是”?为什么选择.alertrowcounter?rowcounter将是令人满意和有效的。@DrewKennedy使用多个选择器有许多非常有效的理由,也许他有一些类似的块,具有不同的类。。。。你对他的其他项目一无所知。是的,我当时就在那,只是希望也许我在某个地方很愚蠢。。。非常感谢您的时间。使用任何比.alertrowcounter更深的选择器都不起作用。您会创建一个JSFIDLE来模拟该行为吗?我想我们会更容易在这方面帮助你。你知道id是唯一契约,所以你不需要在这里使用类。我完全知道id是唯一的,因此我的示例中的类是无关的。但是,它是否起作用并没有什么区别。下面的人也提出了同样的建议,我给了他同样的答案。好吧,让我澄清一件事。div行计数器从一开始就存在,还是在页面加载后出现
$('#rowcounter a.btn').on('click',function() {
    console.log('yes');
    if($(this).data('action') == 'cancel') {
        rowCounter('hide');
    }
    if($(this).data('action') == 'deleterow') {
        var tbl = $(this).data('table');
        var targ = $(this).data('rowid');
        $.get('formhandler.php?handle=deleterow&table='+tbl+'&rowid='+targ,function(d) {
            if(d == 'success') {
                $('#rowcounter').removeClass().addClass('alert alert-success').html('Successfully removed '+targ+'!');
            }
        });
    }
});