Javascript JQuery - 设置属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2009609/
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
JQuery - Set Attribute value
提问by Jake
I have following HTML with two elements having the same name
我有以下两个具有相同名称的元素的 HTML
<input type="hidden" name= "chk0" value="">
<input type="checkbox" name="chk0" value="true" disabled>
Through JQuery, I want to set the enable the checkbox. So, I am using something like this:
通过 JQuery,我想设置启用复选框。所以,我正在使用这样的东西:
$('#chk0').attr("disabled",false);
But this doesn't work. I assume JQuery is getting confused with two elements having the same identical name. Unfortunatel, I cannot avoid using two different names because, when the form is posted, I want all the checkboxes to get posted (not just the ones that are checked). Hence I have to use hidden element with the same name.. So, back to the question, how can I enable the checkbox through JQuery in the above scenario? Is there a "type" parameter for attr which distingues hidden from checkbox?
但这不起作用。我认为 JQuery 与两个具有相同名称的元素混淆了。不幸的是,我无法避免使用两个不同的名称,因为在发布表单时,我希望发布所有复选框(不仅仅是选中的复选框)。因此,我必须使用具有相同名称的隐藏元素.. 所以,回到问题,如何在上述场景中通过 JQuery 启用复选框?attr 是否有“类型”参数可以区分复选框中的隐藏?
Thanks
谢谢
采纳答案by Gabriele Petrioli
Some things before the actual code..
在实际代码之前的一些事情..
the hash (#) you use as the selector is for IDs and not for names of elements. also the disabled attribute is not a true false scenario .. if it has disabled attribute it means that it is true .. you need to remove the attribute and not set it to false. Also there are the form selectors that identify specific types of items in a form ..
您用作选择器的哈希 (#) 用于 ID 而不是用于元素名称。此外, disabled 属性也不是真正的 false 场景 .. 如果它具有 disabled 属性,则意味着它是 true .. 您需要删除该属性而不是将其设置为 false。还有表单选择器可以识别表单中特定类型的项目..
so the code would be
所以代码是
$("input:checkbox[name='chk0']").removeAttr('disabled');
Bringing the answer up-to-date
更新答案
You should use the .prop()method (added since v1.6)
您应该使用该.prop()方法(自 v1.6 起添加)
$("input:checkbox[name='chk0']").prop('disabled', false); // to enable the checkbox
and
和
$("input:checkbox[name='chk0']").prop('disabled', true); // to disable the checkbox
回答by Tim Down
Seriously, just don't use jQuery for this. disabledis a boolean property of form elements that works perfectly in every major browser since 1997, and there is no possible way it could be simpler or more intuitive to change whether or not a form element is disabled.
说真的,只是不要为此使用 jQuery。disabled是表单元素的布尔属性,自 1997 年以来在每个主要浏览器中都能完美运行,并且没有可能更简单或更直观地更改表单元素是否被禁用。
The simplest way of getting a reference to the checkbox would be to give it an id. Here's my suggested HTML:
获取对复选框的引用的最简单方法是给它一个id. 这是我建议的 HTML:
<input type="hidden" name="chk0" value="">
<input type="checkbox" name="chk0" id="chk0_checkbox" value="true" disabled>
And the line of JavaScript to make the check box enabled:
以及启用复选框的 JavaScript 行:
document.getElementById("chk0_checkbox").disabled = false;
If you prefer, you can instead use jQuery to get hold of the checkbox:
如果您愿意,您可以改为使用 jQuery 来获取复选框:
$("#chk0_checkbox")[0].disabled = false;
回答by Hogan
"True" and "False" do not work, to disable, set to value disabled.
“真”和“假”不起作用,要禁用,设置为禁用值。
$('.someElement').attr('disabled', 'disabled');
$('.someElement').attr('disabled', 'disabled');
To enable, remove.
要启用,请删除。
$('.someElement').removeAttr('disabled');
$('.someElement').removeAttr('disabled');
Also, don't worry about multiple items being selected, jQuery will operate on all of them that match. If you need just one you can use many things :first, :last, nth, etc.
另外,不要担心选择了多个项目,jQuery 将对所有匹配的项目进行操作。如果你只需要一个,你可以使用很多东西:first、:last、nth 等。
You are using name and not id as other mention -- remember, if you use id valid xhtml requires the ids be unique.
您正在使用 name 而不是 id 作为其他提及 - 请记住,如果您使用 id 有效 xhtml 要求 ID 是唯一的。
回答by Vincent Ramdhanie
$("#chk0") is refering to an element with the id chk0. You might try adding id's to the elements. Ids are unique even though the names are the same so that in jQuery you can access a single element by it's id.
$("#chk0") 引用了一个 id 为 chk0 的元素。您可以尝试将 id 添加到元素中。即使名称相同,ID 也是唯一的,因此在 jQuery 中您可以通过它的 id 访问单个元素。
回答by leepowers
Use an ID to uniquely identify the checkbox. Your current example is trying to select the checkbox with an id of '#chk0':
使用 ID 来唯一标识复选框。您当前的示例试图选择 ID 为“#chk0”的复选框:
<input type="checkbox" id="chk0" name="chk0" value="true" disabled>
<input type="checkbox" id="chk0" name="chk0" value="true" disabled>
$('#chk0').attr("disabled", "disabled");
$('#chk0').attr("disabled", "disabled");
You'll also need to remove the attribute for disabledto enable the checkbox. Something like:
您还需要删除用于disabled启用复选框的属性。就像是:
$('#chk0').removeAttr("disabled");
$('#chk0').removeAttr("disabled");
See the docs for removeAttr
请参阅removeAttr的文档
The value XHTML for disabling/enabling an input element is as follows:
禁用/启用输入元素的 XHTML 值如下:
<input type="checkbox" id="chk0" name="chk0" value="true" disabled="disabled" />
<input type="checkbox" id="chk0" name="chk0" value="true" />
Note that it's the absenceof the disabled attribute that makes the input element enabled.
请注意,启用 input 元素是因为缺少disabled 属性。
回答by honeybuzzer
You can add different classes to select, or select by type like this:
您可以添加不同的类来选择,或者像这样按类型选择:
$('input[type="checkbox"]').removeAttr("disabled");
$('input[type="checkbox"]').removeAttr("disabled");

