如何使用 jquery/javascript 在 asp.net c# 中禁用单选按钮上的 link 按钮
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('clear');" /><label for="radioBtnQueryQuality_0">One</label></td>
</tr>
<tr>
<td><input id="radioBtnQueryQuality_1" type="radio" name="radioBtnQueryQuality" value="notExpNorImp" onclick="disableButtons('notExpNorImp');" /><label for="radioBtnQueryQuality_1">Two</label></td>
</tr>
<tr>
<td><input id="radioBtnQueryQuality_2" type="radio" name="radioBtnQueryQuality" value="invalid" onclick="disableButtons('invalid');" /><label for="radioBtnQueryQuality_2">Three</label></td>
</tr>
</table>
<a id="btnExcellent" class="btn btn-primary" href="javascript:__doPostBack('btnExcellent','')">Excellent</a>
<a id="btnFine" class="btn btn-primary" href="javascript:__doPostBack('btnFine','')">Fine</a>
<a id="btnBad" class="btn btn-primary" href="javascript:__doPostBack('btnBad','')">Bad</a>
<a id="btnSkip" class="btn btn-primary" href="javascript:__doPostBack('btnSkip','')">Skip</a>
要求:
我想在单击第一个单选按钮时禁用 btnFour 按钮(即值为 one)。
如果我单击其余两个单选按钮,我希望除最后一个按钮外的前三个按钮被禁用。
它没有按我想要的方式工作。如果我采用简单的 HTML 按钮,那么它就可以工作,但对于 LinkButtons 则不起作用。任何帮助将不胜感激。
谢谢。
你可以试试下面的代码,在调用disable函数的时候传递radio button的值,然后启用或禁用相关的按钮
代码片段
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('clear');" /><label for="radioBtnQueryQuality_0">One</label></td>
</tr>
<tr>
<td><input id="radioBtnQueryQuality_1" type="radio" name="radioBtnQueryQuality" value="notExpNorImp" onclick="disableButtons('notExpNorImp');" /><label for="radioBtnQueryQuality_1">Two</label></td>
</tr>
<tr>
<td><input id="radioBtnQueryQuality_2" type="radio" name="radioBtnQueryQuality" value="invalid" onclick="disableButtons('invalid');" /><label for="radioBtnQueryQuality_2">Three</label></td>
</tr>
</table>
<a id="btnExcellent" class="btn btn-primary" href="javascript:__doPostBack('btnExcellent','')">Excellent</a>
<a id="btnFine" class="btn btn-primary" href="javascript:__doPostBack('btnFine','')">Fine</a>
<a id="btnBad" class="btn btn-primary" href="javascript:__doPostBack('btnBad','')">Bad</a>
<a id="btnSkip" class="btn btn-primary" href="javascript:__doPostBack('btnSkip','')">Skip</a>
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('clear');" /><label for="radioBtnQueryQuality_0">One</label></td>
</tr>
<tr>
<td><input id="radioBtnQueryQuality_1" type="radio" name="radioBtnQueryQuality" value="notExpNorImp" onclick="disableButtons('notExpNorImp');" /><label for="radioBtnQueryQuality_1">Two</label></td>
</tr>
<tr>
<td><input id="radioBtnQueryQuality_2" type="radio" name="radioBtnQueryQuality" value="invalid" onclick="disableButtons('invalid');" /><label for="radioBtnQueryQuality_2">Three</label></td>
</tr>
</table>
<a id="btnExcellent" class="btn btn-primary" href="javascript:__doPostBack('btnExcellent','')">Excellent</a>
<a id="btnFine" class="btn btn-primary" href="javascript:__doPostBack('btnFine','')">Fine</a>
<a id="btnBad" class="btn btn-primary" href="javascript:__doPostBack('btnBad','')">Bad</a>
<a id="btnSkip" class="btn btn-primary" href="javascript:__doPostBack('btnSkip','')">Skip</a>
要求:
我想在单击第一个单选按钮时禁用 btnFour 按钮(即值为 one)。 如果我单击其余两个单选按钮,我希望除最后一个按钮外的前三个按钮被禁用。
它没有按我想要的方式工作。如果我采用简单的 HTML 按钮,那么它就可以工作,但对于 LinkButtons 则不起作用。任何帮助将不胜感激。
谢谢。
你可以试试下面的代码,在调用disable函数的时候传递radio button的值,然后启用或禁用相关的按钮
代码片段
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('clear');" /><label for="radioBtnQueryQuality_0">One</label></td>
</tr>
<tr>
<td><input id="radioBtnQueryQuality_1" type="radio" name="radioBtnQueryQuality" value="notExpNorImp" onclick="disableButtons('notExpNorImp');" /><label for="radioBtnQueryQuality_1">Two</label></td>
</tr>
<tr>
<td><input id="radioBtnQueryQuality_2" type="radio" name="radioBtnQueryQuality" value="invalid" onclick="disableButtons('invalid');" /><label for="radioBtnQueryQuality_2">Three</label></td>
</tr>
</table>
<a id="btnExcellent" class="btn btn-primary" href="javascript:__doPostBack('btnExcellent','')">Excellent</a>
<a id="btnFine" class="btn btn-primary" href="javascript:__doPostBack('btnFine','')">Fine</a>
<a id="btnBad" class="btn btn-primary" href="javascript:__doPostBack('btnBad','')">Bad</a>
<a id="btnSkip" class="btn btn-primary" href="javascript:__doPostBack('btnSkip','')">Skip</a>