【发布时间】:2014-11-23 17:33:38
【问题描述】:
我有一个文本输入和一个按钮输入。如果我单击文本输入,并且它不是只读的,则会显示警报“Readonly false”,否则会显示“Readonly true”。按钮输入使文本输入只读,但当我单击文本输入时,'Readonly false' 仍然显示。通过脚本添加只读属性后,Jquery 似乎无法识别选择器。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
<script type="text/javascript" src="jquery/jQuery v1.10.2.js"></script>
<script>
$(document).ready(function() {
$('.makeReadonly').on('click', function() {
$('.foo').prop('readonly', true);
});
$('input.foo:not([readonly])').on('click', function() {
alert('Readonly false');
});
$('input.foo[readonly]').on('click', function() {
alert('Readonly true');
});
});
</script>
</head>
<body>
<input type="button" value="makeReadonly" class="makeReadonly">
<input type="text" class="foo">
</body>
</html>
我能否使用纯粹的选择器触发正确的警报,或者我需要使用 if 语句 (if ($('.foo').prop('readonly')) {alert('Readonly true' )})?
对我来说,这似乎是一个 Jquery 错误,但我想用纯粹的选择器来处理它。但是,如果不可能,我很高兴有人能解释我为什么。
谢谢
【问题讨论】:
标签: javascript jquery html