jQuery implements select all, selects all and gets fields, and deletes specified characters

【Foreword】

   I just added a quick delete function after selecting all to the background. Here I use jquery to achieve it.

   (1) Select All and Select All

   (2) Get the selected data

   (3) Intercept the data, and then pass it to the background

 

【main body】

      If you use jquery, it is best not to use the attr method to add attributes, otherwise the third click will not take effect. It is better to use the prop method. Pay attention to the jquery version when using the prop method, it must be a version after 1.8, otherwise it is not supported

<table class="table table-bordered ">
 <thead class="tab_head">
 <tr>
  <td> <input type="checkbox" onclick="selectAll()"></td>
  <th>Name</th>
  <th>角明</th>
  <th>所统</th>
 </tr>
 </thead>
 <tbody>
 <tr>
  <td> <input type="checkbox"></td>
  <td>系管</td>
  <td>管角</td>
  <td>安台</td>
 </tr>
 <tr>
  <td> <input type="checkbox"></td>
  <td>统管</td>
  <td>员角</td>
  <td>Anping</td>
 </tr>
 <tr>
  <td> <input type="checkbox"></td>
  <td>Administration</td>
  <td>Role</td>
  <td>Wujin</td>
 </tr>
 </tbody>
 </table>
 <script>
 function selectAll(){
 $("input[type='checkbox']").each( function() {
  if($(this).prop("checked")==true) {
  $("input[type='checkbox']").prop('checked', true);
  return;
  } else {
  $("input[type='checkbox']").prop('checked', false);
  return;
  }
 });
 }
 </script>

 The above can complete the select all and no select all

 Get all the selected data below and perform interception processing

$('#batchDel').on('click',function(){
            var idList = '';
            $.each($('input:checkbox:checked'),function(){
                idList+=$(this).val()+',';
            });
            //The output format here will be   on, aaa, bbb, ccc,
            So the next step is to intercept the redundant characters before and after on, and,
            var idListone = idList.substr(0, idList.length - 1);//Remove the end,
            var idListtwo = idListone.replace("on,","");//Remove the first on,
            console.log(idListtwo)
            if(idListtwo == 'on'){
                layer.alert('Please select the article to delete');
            }else if(idListtwo == ''){
                layer.alert('Please select the article to delete');
            }else{
                if(confirm('Confirm delete')){
                    window.location.href = "__MODULE__/Article/del/id/"+idListtwo;
                }
            }
        })

 Supplementary note: The layer used for the pop-up box is rendered

 

 

[Extension] How to remove the specified string in JS

Two ways can be achieved, the first method is recommended here

1: Use the replace function to replace

       var  str="hello world!";

       str=str.replace("l","");

       Even if a string is replaced with an empty string, the function of removing the specified string can be realized.

2: Use the string split function in the aggregation

    var str="hello world!"

    var items=str.split("o")

    Will get an array, the array includes multiple strings divided by o (excluding o)

    var newStr=items.join("");

     Will get a new string, concatenate the arrays in the array using empty strings to form a new string

 

 

 

 

 

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326060607&siteId=291194637