简体   繁体   English

如何使用 jquery/javascript 在 asp.net c# 中禁用单选按钮上的链接按钮单击

[英]How to disable a link button on radio button click in asp.net c# using jquery/javascript

 function disableButtons(val) { if (val == 'clear') { $('#btnExcellent, #btnFine, #btnBad').prop('disabled', true); $('#btnSkip').prop('disabled', false); } else { $('#btnExcellent, #btnFine, #btnBad').removeProp('disabled'); $('#btnSkip').prop('disabled',true); } }
 <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <table id="radioBtnQueryQuality"> <tr> <td><input id="radioBtnQueryQuality_0" type="radio" name="radioBtnQueryQuality" value="clear" onclick="disableButtons(&#39;clear&#39;);" /><label for="radioBtnQueryQuality_0">One</label></td> </tr> <tr> <td><input id="radioBtnQueryQuality_1" type="radio" name="radioBtnQueryQuality" value="notExpNorImp" onclick="disableButtons(&#39;notExpNorImp&#39;);" /><label for="radioBtnQueryQuality_1">Two</label></td> </tr> <tr> <td><input id="radioBtnQueryQuality_2" type="radio" name="radioBtnQueryQuality" value="invalid" onclick="disableButtons(&#39;invalid&#39;);" /><label for="radioBtnQueryQuality_2">Three</label></td> </tr> </table> <a id="btnExcellent" class="btn btn-primary" href="javascript:__doPostBack(&#39;btnExcellent&#39;,&#39;&#39;)">Excellent</a> <a id="btnFine" class="btn btn-primary" href="javascript:__doPostBack(&#39;btnFine&#39;,&#39;&#39;)">Fine</a> <a id="btnBad" class="btn btn-primary" href="javascript:__doPostBack(&#39;btnBad&#39;,&#39;&#39;)">Bad</a> <a id="btnSkip" class="btn btn-primary" href="javascript:__doPostBack(&#39;btnSkip&#39;,&#39;&#39;)">Skip</a>

REQUIREMENT:要求:

I want to disable the btnFour button when I click the first radio button (ie with value one ).当我单击第一个单选按钮(即值为one )时,我想禁用btnFour按钮。 If I click the remaining two radiobuttons, I want first three Buttons to be disabled except the last one.如果我单击剩余的两个单选按钮,我希望除最后一个之外的前三个按钮被禁用。

It doesn't work the way I want.它不像我想要的那样工作。 If I take simple HTML buttons then it works, but it doesn't work in the case of LinkButtons.如果我采用简单的 HTML 按钮,那么它可以工作,但在 LinkBut​​tons 的情况下它不起作用。 Any help would be much appreciated.任何帮助将非常感激。

Thank you.谢谢你。

You can try below code, just pass the value of radio button while calling disable function and make enable or disable of relevant buttons您可以尝试下面的代码,只需在调用禁用功能时传递单选按钮的值并启用或禁用相关按钮

Code Snippet代码片段

 function disableButtons(val) { if (val == 'clear') { $('#btnExcellent, #btnFine, #btnBad').addClass('disabled'); $('#btnSkip').removeClass('disabled'); } else { $('#btnExcellent, #btnFine, #btnBad').removeClass('disabled'); $('#btnSkip').addClass('disabled'); } $('a.disabled').each(function(){ var href = $(this).attr('href'); //console.log('attr = ' + href); $(this).data('href', href); $(this).attr('href', '#') }); $('a:not(.disabled)').each(function(){ var href = $(this).data('href'); //console.log('data = ' + href); if(href) { $(this).attr('href', href); } }); }
 a.disabled { opacity: 0.1; }
 <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <table id="radioBtnQueryQuality"> <tr> <td><input id="radioBtnQueryQuality_0" type="radio" name="radioBtnQueryQuality" value="clear" onclick="disableButtons(&#39;clear&#39;);" /><label for="radioBtnQueryQuality_0">One</label></td> </tr> <tr> <td><input id="radioBtnQueryQuality_1" type="radio" name="radioBtnQueryQuality" value="notExpNorImp" onclick="disableButtons(&#39;notExpNorImp&#39;);" /><label for="radioBtnQueryQuality_1">Two</label></td> </tr> <tr> <td><input id="radioBtnQueryQuality_2" type="radio" name="radioBtnQueryQuality" value="invalid" onclick="disableButtons(&#39;invalid&#39;);" /><label for="radioBtnQueryQuality_2">Three</label></td> </tr> </table> <a id="btnExcellent" class="btn btn-primary" href="javascript:__doPostBack(&#39;btnExcellent&#39;,&#39;&#39;)">Excellent</a> <a id="btnFine" class="btn btn-primary" href="javascript:__doPostBack(&#39;btnFine&#39;,&#39;&#39;)">Fine</a> <a id="btnBad" class="btn btn-primary" href="javascript:__doPostBack(&#39;btnBad&#39;,&#39;&#39;)">Bad</a> <a id="btnSkip" class="btn btn-primary" href="javascript:__doPostBack(&#39;btnSkip&#39;,&#39;&#39;)">Skip</a>

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

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