Javascript 如何使用jQuery的hasClass函数检查表是否有类?

Javascript 如何使用jQuery的hasClass函数检查表是否有类?,javascript,jquery,Javascript,Jquery,我对表b的onclick函数有一个问题,该函数不能确定表a是否有一个已选择的类\u upsize 情景: 表B中有一个项目列表,表a中也有。 现在,当我单击表B中的一个项目时,如果表A有一个选定的_upsize类或没有,单击函数将执行该条件,以缩短场景 如果表A有一个类(selected_upsize),那么它将发出警报,否则我在表B中单击的项目将附加到表A上 我这里有我的输出 我这里有我的函数到我的Onclick函数 $(".customer_edit_table_chaining

我对表b的onclick函数有一个问题,该函数不能确定表a是否有一个已选择的类\u upsize

情景: 表B中有一个项目列表,表a中也有。 现在,当我单击表B中的一个项目时,如果表A有一个选定的_upsize类或没有,单击函数将执行该条件,以缩短场景

如果表A有一个类(selected_upsize),那么它将发出警报,否则我在表B中单击的项目将附加到表A上

我这里有我的输出

我这里有我的函数到我的Onclick函数

     $(".customer_edit_table_chaining_condiments").on('click',function(e){

                        //this is the item of table B that will append if the table A has no class
                        var customer_edit_condimentsScreenNameClicked = $(this).closest('tr').find('.customer_edit_condimentsScreenNameClicked').text();
                        var customer_edit_condimentsScreenPriced = $(this).closest('tr').find('.customer_edit_condimentsScreenPriced').text();

                       $(".customer_edit_table_chaining_condiments").on('click',function(e){

        //this is the item of table B that will append if the table A has no class
        var customer_edit_condimentsScreenNameClicked = $(this).closest('tr').find('.customer_edit_condimentsScreenNameClicked').text();
        var customer_edit_condimentsScreenPriced = $(this).closest('tr').find('.customer_edit_condimentsScreenPriced').text();

       if($('#noun_chaining_order').find('tr.selected_upsize').length){    

            alert('You can"t upsize the item');
       }else{                                        
            $('table#noun_chaining_order').append('<tr class="" id="append_imaginary_upsize_condiments"><td contenteditable="true">-</td><td>'+customer_edit_condimentsScreenNameClicked+'</td><td>'+customer_edit_condimentsScreenPriced+'</td></tr>');
            $('tr#append_imaginary_upsize_condiments').addClass('selected_upsize');

       }
})       
$(“.customer\u edit\u table\u chaining\u调味品”)。在('click',函数(e){
//这是表B的项,如果表A没有类,它将追加该项
var customer_edit_consumentsScreenNameClicked=$(this).closest('tr').find('.customer_edit_consumentsScreenNameClicked').text();
var customer_edit_consuments screenpledged=$(this).closest('tr').find('.customer_edit_consuments screenpledged').text();
$(“.customer\u edit\u table\u chaining\u调味品”)。打开('click',函数(e){
//这是表B的项,如果表A没有类,它将追加该项
var customer_edit_consumentsScreenNameClicked=$(this).closest('tr').find('.customer_edit_consumentsScreenNameClicked').text();
var customer_edit_consuments screenpledged=$(this).closest('tr').find('.customer_edit_consuments screenpledged').text();
if($('#名词_链接_顺序').find('tr.selected_upsize').length){
警报('您无法放大项目');
}否则{
$('table#名词_chaining _order')。追加('-'+客户_编辑_调味品筛选点击+''+客户_编辑_调味品筛选定价+'');
$('tr#append_virtual_upsize_调味品')。addClass('selected_upsize');
}
})       
我的Html表格A

<table class="table table-hover upsize_check" id="noun_chaining_order" style="border:none;">
<thead>
    <tr style="font-size: 15px;  color:white;">
        <th scope="col">Qty</th>
        <th scope="col">Condiments</th>
        <th scope="col">Price</th>
    </tr>
</thead>
<tbody style="font-size:14px; color:white;" class="tbody_noun_chaining_order">                  

</tbody>

数量
调味品
价格

表B

<table  class="table table-striped table-bordered " id="customer_table_update_chain_order" style="width:100%">
<div class="content-noun" style="text-align: center;">
<thead>
    <tr style="background: linear-gradient(-20deg, #00e4d0, #5983e8); color:white;" >
        <th>Condiment Screen Name</th>
         <th>Condiment Price</th>
         <th>Condiment Image</th>
    </tr>
</thead>
    </div>
<tbody>

</tbody>

调味品网名
调味品价格
调味品形象
$(“.customer\u edit\u table\u chaining\u调味品”)。在('click',函数(e){
//这是表B的项,如果表A没有类,它将追加该项
var customer_edit_consumentsScreenNameClicked=$(this).closest('tr').find('.customer_edit_consumentsScreenNameClicked').text();
var customer_edit_consuments screenpledged=$(this).closest('tr').find('.customer_edit_consuments screenpledged').text();
//这里将检查表A是否选择了“升迁”
//如果我们已经选择了\u UPSIZE类,那么下一行将返回所有元素。在FIND方法中选择了\u UPSIZE,因此下一行将始终为TRUE
$('table#noon(名词)chaining(顺序)')。find('.upsize(检查)')。each(function(){
//相反,如果您在上面的代码行中搜索具有不同类的元素,则可能会或可能不会有所选的_UPSIZE类,因此使下面的语句合法
if($(this).hasClass('selected_upsize')){
警报('您无法放大项目');
}
其他的
{
$('table#名词_chaining _order')。追加('-'+客户_编辑_调味品筛选点击+''+客户_编辑_调味品筛选定价+'');
$('tr#append_virtual_upsize_调味品')。addClass('selected_upsize');
}
});
});
正在下面添加解决方案

HTML


数量
调味品
价格
调味品网名
调味品价格
调味品形象
JAVASCRIPT

$("#customer_table_update_chain_order tbody tr").on('click',function(e){

                //this is the item of table B that will append if the table A has no class
                var customer_edit_condimentsScreenNameClicked = $(this).find('.customer_edit_condimentsScreenNameClicked').text();
                var customer_edit_condimentsScreenPriced = $(this).find('.customer_edit_condimentsScreenPriced').text();

                //here will check if table A has selected upsize or not
                        $('table#noun_chaining_order tbody tr').each(function(){
                    if ($(this).hasClass('selected_upsize')) {

                    sameRow = true;
                  }
                  else
                  {
                    sameRow =false;
                    $('table#noun_chaining_order').append('<tr class="" id="append_imaginary_upsize_condiments"><td contenteditable="true">-</td><td>'+customer_edit_condimentsScreenNameClicked+'</td><td>'+customer_edit_condimentsScreenPriced+'</td></tr>');
                    $('tr#append_imaginary_upsize_condiments').addClass('selected_upsize');
                  }

                    });


            if(sameRow){
                alert('You can"t upsize the item');
            }

              });
$(“#客户(表)更新(链)订单(体tr)”)。在('click',函数(e){
//这是表B的项,如果表A没有类,它将追加该项
var customer_edit_consumentsScreenNameClicked=$(this).find('.customer_edit_consumentsScreenNameClicked').text();
var customer_edit_consuments screenpledged=$(this).find('.customer_edit_consuments screenpledged').text();
//这里将检查表A是否选择了“升迁”
$('table#名词_chaining _ordertbody tr')。每个(函数(){
if($(this).hasClass('selected_upsize')){
sameRow=真;
}
其他的
{
sameRow=假;
$('table#名词_chaining _order')。追加('-'+客户_编辑_调味品筛选点击+''+客户_编辑_调味品筛选定价+'');
$('tr#append_virtual_upsize_调味品')。addClass('selected_upsize');
}
});
如果(sameRow){
警报('您无法放大项目');
}
});
请试试这个。 如果我的理解是正确的,这应该是可行的

$(".customer_edit_table_chaining_condiments").on('click',function(e){

        //this is the item of table B that will append if the table A has no class
        var customer_edit_condimentsScreenNameClicked = $(this).closest('tr').find('.customer_edit_condimentsScreenNameClicked').text();
        var customer_edit_condimentsScreenPriced = $(this).closest('tr').find('.customer_edit_condimentsScreenPriced').text();

       if($('#noun_chaining_order').find('tr.selected_upsize').length){    

            alert('You can"t upsize the item');
       }else{                                        
            $('table#noun_chaining_order').append('<tr class="" id="append_imaginary_upsize_condiments"><td contenteditable="true">-</td><td>'+customer_edit_condimentsScreenNameClicked+'</td><td>'+customer_edit_condimentsScreenPriced+'</td></tr>');
            $('tr#append_imaginary_upsize_condiments').addClass('selected_upsize');

       }
})                  
$(“.customer\u edit\u table\u chaining\u调味品”)。在('click',函数(e){
//这是表B的项,如果表A没有类,它将追加该项
var customer_edit_consumentsScreenNameClicked=$(this).closest('tr').find('.customer_edit_consumentsScreenNameClicked').text();
var customer_edit_consuments screenpledged=$(this).closest('tr').find('.customer_edit_consuments screenpledged').text();
if($('#名词_链接_顺序').find('tr.selected_upsize').length){
警报('您无法放大项目');
}否则{
$('table#名词_chaining _order')。追加('-'+客户_编辑_调味品筛选点击+''+客户_编辑_调味品筛选定价+'');
$('tr#append_virtual_upsize_调味品')。addClass('selected_upsize'))
$("#customer_table_update_chain_order tbody tr").on('click',function(e){

                //this is the item of table B that will append if the table A has no class
                var customer_edit_condimentsScreenNameClicked = $(this).find('.customer_edit_condimentsScreenNameClicked').text();
                var customer_edit_condimentsScreenPriced = $(this).find('.customer_edit_condimentsScreenPriced').text();

                //here will check if table A has selected upsize or not
                        $('table#noun_chaining_order tbody tr').each(function(){
                    if ($(this).hasClass('selected_upsize')) {

                    sameRow = true;
                  }
                  else
                  {
                    sameRow =false;
                    $('table#noun_chaining_order').append('<tr class="" id="append_imaginary_upsize_condiments"><td contenteditable="true">-</td><td>'+customer_edit_condimentsScreenNameClicked+'</td><td>'+customer_edit_condimentsScreenPriced+'</td></tr>');
                    $('tr#append_imaginary_upsize_condiments').addClass('selected_upsize');
                  }

                    });


            if(sameRow){
                alert('You can"t upsize the item');
            }

              });
$(".customer_edit_table_chaining_condiments").on('click',function(e){

        //this is the item of table B that will append if the table A has no class
        var customer_edit_condimentsScreenNameClicked = $(this).closest('tr').find('.customer_edit_condimentsScreenNameClicked').text();
        var customer_edit_condimentsScreenPriced = $(this).closest('tr').find('.customer_edit_condimentsScreenPriced').text();

       if($('#noun_chaining_order').find('tr.selected_upsize').length){    

            alert('You can"t upsize the item');
       }else{                                        
            $('table#noun_chaining_order').append('<tr class="" id="append_imaginary_upsize_condiments"><td contenteditable="true">-</td><td>'+customer_edit_condimentsScreenNameClicked+'</td><td>'+customer_edit_condimentsScreenPriced+'</td></tr>');
            $('tr#append_imaginary_upsize_condiments').addClass('selected_upsize');

       }
})