javascript 在 css 类中禁用控件的不同方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15221910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Different ways of disabling controls within a css class
提问by Mathematics
Can someone tell me all possible ways to disable all buttons in a class, I already tried this which isn't working,
有人能告诉我禁用类中所有按钮的所有可能方法吗,我已经尝试过这个不起作用,
$("input.myClass").attr('disabled', true);
This is how I am using it,
这就是我使用它的方式
<script type="text/javascript">
$(document).ready(function() {
var objA = $get('<%= checkbox.ClientID %>');
$(objA).change(function() {
if($(this).is(':checked'))
{
$(".anotherClass").sortable('disable'); //works
$("input.myClass").prop('disabled', 'disabled'); //doesn't work
}
CSS is quiet simple,
CSS很简单,
.myClass
{
float: left;
width: 140px;
margin-left: 4px;
margin-right: 4px;
padding-top: 20px;
text-align: center;
}
HTML looks like this,
HTML看起来像这样,
<div class="myClass">
<asp:Button ID="btn1" Text="bbb" runat="server" CausesValidation="False" CssClass="button" OnClientClick="return false;" />
<asp:Button ID="btn2" Text="aaa" runat="server" CausesValidation="False" CssClass="button" OnClientClick="return false;" />
<asp:Button ID="btn3" runat="server" CausesValidation="False" CssClass="button" OnClick="bt_Click" />
</div>
采纳答案by Mathematics
$(".myClass :input").attr('disabled', true);
is the only code that worked for me.
是唯一对我有用的代码。
回答by Sanober Malik
jQuery 1.6+
jQuery 1.6+
To change the disabled property you should use the .prop() function.
要更改禁用属性,您应该使用 .prop() 函数。
$("input.myclass").prop('disabled', true);
$("input.myclass").prop('disabled', false);
jQuery 1.5 and below
jQuery 1.5 及以下
The .prop() function doesn't exist, but .attr() does similar:
.prop() 函数不存在,但 .attr() 有类似的作用:
Set the disabled attribute.
设置禁用属性。
$("input.myclass").attr('disabled','disabled');
To enable again
再次启用
$("input.myclass").removeAttr('disabled');
Referred From : Stack Question
引用自:堆栈问题
回答by Matthias Wegtun
since you asked all possible ways and the common ones are out there you can always prevent default by doing:
由于您询问了所有可能的方法并且常见的方法都在那里,因此您始终可以通过执行以下操作来防止默认:
$('input.myClass').click(function(e){
e.preventDefault();
})
preventing default, prevents from submitting a form for example.
防止默认,例如防止提交表单。
回答by web2008
You can use $("input[type='button']").attr("disabled","disabled"); to disable all buttons.
您可以使用 $("input[type='button']").attr("disabled","disabled"); 禁用所有按钮。
回答by Aleksandr M
回答by dsgriffin
If you want to use attr()
, use this:
如果你想使用attr()
,使用这个:
$("input.myClass").attr('disabled', 'disabled');
Or prop()
:
或prop()
:
$("input.myClass").prop('disabled', true);
In regard to updated question:
关于更新的问题:
- Change your script tag to
<script>
- Place it towards the bottom of the page (not in the head section)
- 将您的脚本标签更改为
<script>
- 将其放在页面底部(而不是头部部分)
回答by ????
To disable
禁用
$('input.myClass').attr('disabled', 'disabled');
// To enable
// 启用
$('input.myClass').removeAttr('disabled');
// OR you can set attr to ""
// 或者你可以将 attr 设置为 ""
$('input.myClass').attr('disabled', '');
Some SO Links
jQuery disable/enable submit button
JQuery - Set Attribute value
.attr("disabled", "disabled") issue
Toggle input disabled attribute using jQuery
一些 SO 链接
jQuery 禁用/启用提交按钮
JQuery - 设置属性值
.attr("disabled", "disabled") 问题
使用 jQuery 切换输入禁用属性