2013-11-21 20 views
0

如何限制基於複選框條件點擊一個href鏈接(鏈接只有在選中後才能使用)?如何限制基於複選框條件的href鏈接點擊?

<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['id'] . '"'.($row['pr'] == ""?"disabled ":"").' style="cursor:pointer;" class="checkbox"></td> 

<a class="button" href="javascript:document.forms[0].submit();" onclick="f1.action='editpr.php'; return true;"> 
<span><b>Edit Purchase Request</b></span></a> 

<a class="button" href="javascript:document.forms[0].submit();"><span><b>Remove Purchase Request</b></span></a> 

我真的有這個麻煩。幫助

回答

1

,取而代之的是jQuery的點擊處理程序爲條件

<a class="button submit"><span><b>Edit Purchase Request</b></span></a> 
<a class="button remove"><span><b>Remove Purchase Request</b></span></a> 

然後

jQuery(function ($) { 
    $('a.button.submit, a.button.remove').click(function() { 
     if ($('input[name="checkbox[]"]:checked').length == 0) { 
      return; 
     } 

     var frm = document.f1; 
     if($(this).hasClass('submit')){ 
      frm.action = 'editpr.php'; 
     } 
     frm.submit(); 
    }) 
}) 
取出內嵌點擊處理,然後在處理程序檢查

注意:看起來所有複選框元素都有ID checkbox[],這是不允許的,因爲一個元素的ID必須是唯一的

+0

問題是我需要在我的PHP過程中的複選框[]。 – surname

+0

@papaosurname所以...它應該工作...我希望'f1'是包含複選框的表單的名稱 –

+0

是的f1是我的表單的名稱。它的工作原理。太好了! – surname

0

這裏是jsfiddle link

//This is for set the href on page load 
settingHref(); 
//This is for set the herf on chage of checkbox 
$('#checkbox').change(function() { 
    settingHref(); 
}); 
function settingHref() { 
    if ($('#checkbox').is(":checked")) { 
     $('.button').attr('href', 'javascript:document.forms[0].submit();'); 
    } 
    else { 
     $('.button').attr('href', 'javascript: void(0)'); 
    } 
}