使用JavaScript为切换按钮事件添加图标

huangapple go评论64阅读模式
英文:

Add Icon to toggle button event using JavaScript

问题

$('#sel_button').html('<i class="glyphicon glyphicon-trash"></i>Deleted all selected('+$('.customer_checkbox:checked').length+')');
英文:
function toggledDeleteAllBtn(){
        if( $('.customer_checkbox:checked').length > 0){
            $('#sel_button').text('Deleted all selected('+$('.customer_checkbox:checked').length+')');
          
        }else{
            $('#sel_button').text('Delete all selected');
        }

        
    }
            <tr id="sid">
                <th>ID</th>
                <th>phone number</th>
                <th>remarks</th>
                <th>date</th>
                <th></th>
                <th><input type="checkbox" name="main-checkbox"></th>
                <th><button class="btn btn-danger btn-sm" id="sel_button" name="sel_button" ><i class="glyphicon glyphicon-trash" id="trash_icon"></i>Delete all </button></th>
        

            </tr>

May I know how to add icon trash in iclass to the function toggleDeleteAllBtn() so that the icon will always be shown up no matter what changes have been done? (need to make some modifications in toggleDeleteAllBtn())
thank you!

答案1

得分: 0

I used back my preferred icon by taking Mamun's answer as the references. (Just realized that Mamun has deleted his original answer since he is using font awesome instead of glyphicon. Anyway, thanks very much for giving me the ideas!)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
function toggledDeleteAllBtn() {
  if ($('.customer_checkbox:checked').length > 0) {
    $('#sel_button').html('<i class="glyphicon glyphicon-trash"></i>Delete all selected(' + $('.customer_checkbox:checked').length + ')');
  } else {
    $('#sel_button').html('<i class="glyphicon glyphicon-trash"></i>Delete all selected');
  }
}
$(document).on('click', '.customer_checkbox', toggledDeleteAllBtn);

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"></link>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script src="plugins/Jquery-Table-Check-All/dist/TableCheckAll.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

<table>
  <tr id="sid">
    <th> ID </th>
    <th> phone number </th>
    <th> remarks </th>
    <th> date </th>
    <th></th>
    <th><input type="checkbox" class="customer_checkbox" name="main-checkbox"></th>
    <th><button class="btn btn-danger btn-sm" id="sel_button" name="sel_button"><i class="glyphicon glyphicon-trash"></i> Delete all selected</button></th>
  </tr>
</table>

<!-- end snippet -->

(Note: The code portion is not translated as per your request.)

英文:

I used back my preferred icon by taking Mamun's answer as the references. (Just realised that Mamun has deleted his original answer since he is using font awesome instead of glyphicon. Anyway, thanks very much for giving me the ideas!)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function toggledDeleteAllBtn() {
  if ($(&#39;.customer_checkbox:checked&#39;).length &gt; 0) {
    $(&#39;#sel_button&#39;).html(&#39;&lt;i class=&quot;glyphicon glyphicon-trash&quot; &lt;/i&gt;Delete all selected(&#39; + $(&#39;.customer_checkbox:checked&#39;).length + &#39;)&#39;);
  } else {
    $(&#39;#sel_button&#39;).html(&#39;&lt;i class=&quot;glyphicon glyphicon-trash&quot; &lt;/i&gt;Delete all selected&#39;);
  }
}
$(document).on(&#39;click&#39;, &#39;.customer_checkbox&#39;, toggledDeleteAllBtn);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;
&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css&quot;&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js&quot;&gt;
&lt;/script&gt;
&lt;script src=&quot;plugins/Jquery-Table-Check-All/dist/TableCheckAll.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js&quot;&gt;
&lt;/script&gt;

&lt;table&gt;
  &lt;tr id=&quot;sid&quot;&gt;
    &lt;th&gt; ID &lt;/th&gt;
    &lt;th&gt; phone number &lt;/th&gt;
    &lt;th&gt; remarks &lt;/th&gt;
    &lt;th&gt; date &lt;/th&gt;
    &lt;th&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;checkbox&quot; class=&quot;customer_checkbox&quot; name=&quot;main-checkbox&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;button class=&quot;btn btn-danger btn-sm&quot; id=&quot;sel_button&quot; name=&quot;sel_button&quot;&gt;&lt;i class=&quot;glyphicon glyphicon-trash&quot; &lt;/i&gt; Delete all selected&lt;/button&gt;&lt;/th&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月7日 17:36:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定