嗨, friend 们,这里是我使用 native JavaScript 编写的代码。但现在我想在 jQuery 中做同样的事情。
html -
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<form>
<fieldset>
<legend>Suscribe</legend>
<p><input id='newsYes' type='checkbox' /> Please send me monthly discount.</p>
<p>Email: <input id='newsEmail' type='text' placeholder='your email' disabled/></p>
</fieldset>
</form>
</body>
</html>
css -
#newsEmail {
width: 175px;
}
#newsEmail:disabled {
background: #999;
}
#newsEmail:enabled {
background: #fff;
}
javascript -
var elem = document.getElementById('newsYes');
elem.onclick = function(){
if(elem.checked){
document.getElementById('newsEmail').disabled = false;
}else {
document.getElementById('newsEmail').disabled = true;
}
};
这是有效的 javascript jsbin 链接 - http://jsbin.com/vumuf/1/edit?html,css,js,output
最佳答案
您可以使用.click() 用于 jQuery 中的点击事件以及 .prop() 设置输入
的状态:
$('#newsYes').click(function () {
$('#newsEmail').prop('disabled', !this.checked)
})
<强> Fiddle Demo
关于jquery - 使用 jQuery 禁用文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22829039/