单击时启用和禁用输入字段(不切换)
Enable and Disable Input Field On Click (not toggle)
问:如何在单击锚标记时禁用一个字段然后启用另一个字段。
目前的进展:禁用该字段有效,但不幸的是,重新启用该字段只需再单击一次即可。此外,点击时不会启用第二个字段。
// When clicking "Lock your username" that input field should be disabled,
//and the Comment section should be enabled
$("#lock").click(function() {
$("#sender").attr('disabled', !$("#sender").attr('disabled'));
$("#message").attr('disabled', !$("#message").attr('enabled'));
});
//problems right now:
// clicking twice re-enables the field.
// from what i've understood, disabled fields cannot be targeted with event handlers?
//
//
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formInput">
<input type="text" id="sender" placeholder="Your Name" />
<a id="lock" style="border:1px solid rgba(000,200,000,0.5);background:#ccc;cursor:pointer;padding:2px;width:150px">Lock your username</a>
<div id="messages" style="height:100px; border:1px solid #000;width:200px;">
Messages will go here
</div>
<input type="text" id="message" placeholder="write your comment here" autocomplete="off" disabled />
<input type="submit" id="send" style="border:1px solid rgba(000,200,000,0.5);background:#ccc;cursor:pointer;padding:2px;width:150px" value="send message" />
</form>
您可以使用 prop()
回调函数,这将切换禁用 属性
$("#lock").click(function(e) {
$("#sender,#message").prop('disabled', function(i, v) {
return !v;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="formInput">
<input type="text" id="sender" placeholder="Your Name" />
<a id="lock" style="border:1px solid rgba(000,200,000,0.5);">Lock your username</a>
<div id="messages" style="height:100px; border:1px solid #000;width:200px;">
</div>
<input type="text" id="message" placeholder="comment here" autocomplete="off" disabled/>
<input type="button" id="send" value="send message" />
</form>
如果你只想执行一次,那么使用one()
$("#lock").one('click', function(e) {
$("#sender,#message").prop('disabled', function(i, v) {
return !v;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="formInput">
<input type="text" id="sender" placeholder="Your Name" />
<a id="lock" style="border:1px solid rgba(000,200,000,0.5);">Lock your username</a>
<div id="messages" style="height:100px; border:1px solid #000;width:200px;">
</div>
<input type="text" id="message" placeholder="comment here" autocomplete="off" disabled/>
<input type="button" id="send" value="send message" />
</form>
这应该有帮助:
$("#lock").click(function(){
$("#sender").attr('disabled', !$("#sender").attr('disabled'));
$("#message").attr('disabled', !$("#message").attr('disabled'));
});
工作Fiddle:http://jsfiddle.net/4u5yuu6f/
对于 Jquery 1.6+ 你可以使用
$("input").prop('disabled', true);
$("input").prop('disabled', false);
如果您只想执行一次,您可以在单击后删除处理程序。像这样:
var lockHandler = function () {
$('#sender').attr('disabled', 'disabled');
$('#message').attr('disabled', null);
$('#lock').off('click', lockHandler);
}
$('#lock').on('click', lockHandler);
可能还想检查以确保在删除之前输入了真实的用户名。
问:如何在单击锚标记时禁用一个字段然后启用另一个字段。
目前的进展:禁用该字段有效,但不幸的是,重新启用该字段只需再单击一次即可。此外,点击时不会启用第二个字段。
// When clicking "Lock your username" that input field should be disabled,
//and the Comment section should be enabled
$("#lock").click(function() {
$("#sender").attr('disabled', !$("#sender").attr('disabled'));
$("#message").attr('disabled', !$("#message").attr('enabled'));
});
//problems right now:
// clicking twice re-enables the field.
// from what i've understood, disabled fields cannot be targeted with event handlers?
//
//
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formInput">
<input type="text" id="sender" placeholder="Your Name" />
<a id="lock" style="border:1px solid rgba(000,200,000,0.5);background:#ccc;cursor:pointer;padding:2px;width:150px">Lock your username</a>
<div id="messages" style="height:100px; border:1px solid #000;width:200px;">
Messages will go here
</div>
<input type="text" id="message" placeholder="write your comment here" autocomplete="off" disabled />
<input type="submit" id="send" style="border:1px solid rgba(000,200,000,0.5);background:#ccc;cursor:pointer;padding:2px;width:150px" value="send message" />
</form>
您可以使用 prop()
回调函数,这将切换禁用 属性
$("#lock").click(function(e) {
$("#sender,#message").prop('disabled', function(i, v) {
return !v;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="formInput">
<input type="text" id="sender" placeholder="Your Name" />
<a id="lock" style="border:1px solid rgba(000,200,000,0.5);">Lock your username</a>
<div id="messages" style="height:100px; border:1px solid #000;width:200px;">
</div>
<input type="text" id="message" placeholder="comment here" autocomplete="off" disabled/>
<input type="button" id="send" value="send message" />
</form>
如果你只想执行一次,那么使用one()
$("#lock").one('click', function(e) {
$("#sender,#message").prop('disabled', function(i, v) {
return !v;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="formInput">
<input type="text" id="sender" placeholder="Your Name" />
<a id="lock" style="border:1px solid rgba(000,200,000,0.5);">Lock your username</a>
<div id="messages" style="height:100px; border:1px solid #000;width:200px;">
</div>
<input type="text" id="message" placeholder="comment here" autocomplete="off" disabled/>
<input type="button" id="send" value="send message" />
</form>
这应该有帮助:
$("#lock").click(function(){
$("#sender").attr('disabled', !$("#sender").attr('disabled'));
$("#message").attr('disabled', !$("#message").attr('disabled'));
});
工作Fiddle:http://jsfiddle.net/4u5yuu6f/
对于 Jquery 1.6+ 你可以使用
$("input").prop('disabled', true);
$("input").prop('disabled', false);
如果您只想执行一次,您可以在单击后删除处理程序。像这样:
var lockHandler = function () {
$('#sender').attr('disabled', 'disabled');
$('#message').attr('disabled', null);
$('#lock').off('click', lockHandler);
}
$('#lock').on('click', lockHandler);
可能还想检查以确保在删除之前输入了真实的用户名。