[英]Button disable/enable onclick
我有2个这样的按钮:
$body .= '<a href="#" onclick="check();" id="check">Check </i></a>';
$body .= '<a href="#" onclick="continue();" id="cont">Continue </a>';
我希望如果用户不单击“检查”按钮,则“继续”按钮被禁用。
我有这个 :
$('#cont').click(function(){return false; });
$('#check').click(function() {
$('#cont').unbind('click');
});
但是,当我单击“继续”按钮(单击检查之前)时,href无效,但是onclick可以正常工作! 如何使用href禁用onclick。 谢谢
你可以用这个
$body .= '<a href="#" onclick="check();" id="check">Check</a>';
$body .= '<a href="#" onclick="continue();" id="cont" disabled>Continue </a>';
和jQuery代码
$(document).ready(function(){
$('[name^="check"]').change(function() {
if( $('#cont').prop('disabled'))
$('#cont').prop('disabled',false);
else
$('#cont').prop('disabled',true);
});
});
您可以使用此链接http://jsfiddle.net/DC3EH/101/
从两个按钮中删除onclick属性,并使用以下代码(如果可行)
最初禁用继续按钮
$body .= '<a href="#" id="check">Check</a>';
$body .= '<a href="#" id="cont" disabled="disabled">Continue </a>';
$('#check').click(function(e) {
e.preventDefault();
$('#cont').prop({disabled:false});
});
我的HTML:
<a href="#" onclick="check();" id="check">Check </i></a>
<a href="#" onclick="continueX();" id="cont">Continue </a>
我的JS:
function check(){
//console.log('Check >>>');
//Whatever you run...
}
function continueX(){
//console.log('continue >>>');
//Whatever you run...
}
$('#cont').each(function(){
//block clicking:
$(this).click(function(e){e.preventDefault(); return false});
//Store its current inline onclick property;
$(this).data('onclick', this.onclick);
//add a class to identify itself:
$(this).addClass('disabled');
//Re-define it:
this.onclick = function(event) {
//Restrict click if it has disabled class:
if( $(this).hasClass('disabled') ){
return false;
};
//otherwise, call inline onclick:
$(this).data('onclick').call(this, event || window.event);
};
});
$('#check').click(function(){
//now allow click:
$('#cont').unbind('click').removeClass('disabled');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.