简体   繁体   English

选中复选框时删除链接

[英]remove link when checkbox is checked

I have a table with Datatable plugin, and I did the part when the user clicks on the row ( tr ) that he will be redirected to that link. 我有一个带有Datatable插件的表,当用户单击行( tr )时,我将执行该操作,他将被重定向到该链接。 but i don't want the user to be redirected to the link when he clicks the checkbox on the row. 但我不希望用户单击该行上的复选框时将其重定向到该链接。 Here is the html: 这是html:

 <tr class="odd gradeX">
   <td class="number_elem_lang">
      <label class='with-square-checkbox2-mylist-details'>
      <input type='checkbox'>
      <span></span>
      </label>
   </td>
   <td class=""> ID022ox</td>
   <td class="list-name">First Lipsum List</td>
   <td class=""> 22 Candidates</td>
   <td class="">01 Apr 2016</td>
   <td><a href=""></a></td>
</tr>

Here is my javascript code for redirecting the user to the link when it's clicked: 这是我的JavaScript代码,用于在用户单击链接时将其重定向到该链接:

$('#sample_1').on( 'click', 'tr', function() {
  var $a = $(this).find('a').last();
  if ( $a.length )
     window.location = $a.attr('href');
  } );

So i don't want to redirect the user when the checkbox is clicked, pls help :) 所以我不想在单击复选框时重定向用户,请帮助:)

Thank you 谢谢

You can use e.target to check what element the user clicked on. 您可以使用e.target来检查用户单击了哪些元素。 In the example below, we check if the user clicked on an input of type checkbox . 在下面的示例中,我们检查用户是否单击了类型为checkboxinput Then we don't run the rest of the function. 然后,我们不运行其余功能。

 $('table').on( 'click', 'tr', function(e) { var target = $(e.target); debugger; // For debugging purposes. if (target.is('input[type=checkbox]')) { // Do not continue if it's an input console.log('no redirect'); return true; } console.log('do redirect'); var $a = $(this).find('a').last(); if ( $a.length ) window.location = $a.attr('href'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr class="odd gradeX"> <td class="number_elem_lang"> <label class='with-square-checkbox2-mylist-details'> <input type='checkbox'> <span></span> </label> </td> <td class=""> ID022ox</td> <td class="list-name">First Lipsum List</td> <td class=""> 22 Candidates</td> <td class="">01 Apr 2016</td> <td><a href=""></a></td> </tr> </table> 

Update: 更新:

In this particular case the checkbox had some custom styling, which led to e.target being a span . 在此特定情况下,复选框具有一些自定义样式,从而导致e.targetspan The solution is to change the condition to $(e.target).is('span') , or even better set a class on the span and use $(e.target).hasClass('my-custom-checkbox') . 解决方案是将条件更改为$(e.target).is('span') ,甚至更好地在span上设置一个类并使用$(e.target).hasClass('my-custom-checkbox')

Here you go - Add this to cancel the event when the checkbox is clicked: 您可以在此处-添加此内容以在单击复选框时取消事件:

 $("tr input:checkbox").click(function(event) {
     event.stopPropagation();
     // Do something
 });

Here is a working Demo 这是一个正在工作的演示

 $('table').on('click', 'tr', function() { var $a = $(this).find('a').last(); if ($a.length) alert("fsfsd"); }); $("tr input:checkbox").click(function(event) { event.stopPropagation(); // Do something }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr class="odd gradeX"> <td class="number_elem_lang"> <label class='with-square-checkbox2-mylist-details'> <input type='checkbox'> <span></span> </label> </td> <td class="">ID022ox</td> <td class="list-name">First Lipsum List</td> <td class="">22 Candidates</td> <td class="">01 Apr 2016</td> <td> <a href=""></a> </td> </tr> </table> 

Use the if construction 使用if构造

if ($('input.checkbox_check').is(':checked')) {
...
}
 Add a class named yourradio in your radio button and add this javascript 
<label class='with-square-checkbox2-mylist-details'>
      <input type='checkbox' class="yourradio">
      <span></span>
      </label> 
<script>
$('.yourradio').on('click', function(){
 return false;
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM