javascript Jquery 道具功能未按预期工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12387786/
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 prop function is not working as expected
提问by Umesh Awasthi
I am trying to enable/disable some hidden fields based on some calculation and using jquery
prop
function, here is the code
我正在尝试基于某些计算和使用 jqueryprop
函数启用/禁用一些隐藏字段
,这是代码
function enableSelectedFieldsData(count, mapKey, index) {
$("#code_" + mapKey + "_" + index).prop("disabled", false);
$("#description_" + mapKey + "_" + index).prop("disabled", false);
$("#crossRefrence_" + mapKey + "_" + index).prop("disabled", false);
$("#image_" + mapKey + "_" + index).prop("disabled", false);
$("#price_" + mapKey + "_" + index).prop("disabled", false);
// disable all other fields
for (var i = 0; i < count; i++) {
if (i != index) {
$("#code_" + mapKey + "_" + i).prop("disabled", true);
$("#description_" + mapKey + "_" + i).prop("disabled", true);
$("#crossRefrence_" + mapKey + "_" + i).prop("disabled", true);
$("#image_" + mapKey + "_" + i).prop("disabled", true);
$("#price_" + mapKey + "_" + i).prop("disabled", true);
}
}
}
Initially i am setting disable=true for all fields and based on the selection i m trying to enable selected fields while disabling other fields, since as per my knowledge disable fields never got submitted to the server on submitting the form, but in my case they are getting submitted.
最初我为所有字段设置 disable=true 并基于选择我试图在禁用其他字段的同时启用选定的字段,因为据我所知,禁用字段在提交表单时从未提交到服务器,但在我的情况下它们是被提交。
on checking using firebug i saw that the disable field value for non selected item is getting set as ""
like disable=""
使用萤火虫,我看到非选定项禁用字段的值越来越集作为检查""
像disable=""
i am not sure where i am setting things wrong, any help or pointer in this regard will really be helpful.
我不确定我在哪里设置错误,这方面的任何帮助或指示都会有所帮助。
Edit
编辑
I have taken out the relevant section from my generated HTML and placed it at jsfiddleplease have a look
我已经从我生成的 HTML 中取出相关部分并将其放在jsfiddle请看一下
回答by Nope
Do you have prop() available?
你有可用的 prop() 吗?
prop()
was added in jQuery 1.6and is used like this:
prop()
在jQuery 1.6中添加并使用如下:
$("input").prop('disabled', true);
$("input").prop('disabled', false);
If you are using jQuery 1.5.xor lower you can use attr()
instead as seen in this FAQ - How to enable/disable form elementsfrom the jQuery site:
如果您使用的是jQuery 1.5.x或更低版本,则可以改用attr()
此常见问题解答 - 如何从 jQuery 站点启用/禁用表单元素:
// Disable #x
$('#x').attr('disabled', true);
// Enable #x
$('#x').attr('disabled', false);
// -- or --
// Disable #x
$("#x").attr('disabled', 'disabled');
// Enable #x
$("#x").removeAttr('disabled');
Assuming you are using jQuery 1.6 or higher
假设您使用的是 jQuery 1.6 或更高版本
Your syntax looks fine. I would guess your problem is then most likely incorrect selectors.
你的语法看起来不错。我猜你的问题很可能是不正确的选择器。
To validate the selector contains the element reference you expect do:
要验证选择器包含您期望的元素引用,请执行以下操作:
// output the selector to the console
console.log($("#code_" + mapKey + "_" + index));
If you see an element in your browser's debugging console you are looking at a valid selector, if instead you see []
the your selector is invalid.
如果您在浏览器的调试控制台中看到一个元素,您正在查看一个有效的选择器,如果您看到[]
您的选择器无效。
Alternatively you can check it using the length
property and alert that out:
或者,您可以使用length
属性检查它并提醒它:
// alert out the length of the jQuery selector
alert($("#code_" + mapKey + "_" + index).length);
If you see 0
then your selector is invalid, if you see 1
or more then your selector is correct.
如果你看到0
那么你的选择器是无效的,如果你看到1
或更多那么你的选择器是正确的。
回答by Anthony Grist
The disabled
attribute in HTML is a bit different to (most) other attributes, in that its presence alone is enough to disable the element.
disabled
HTML 中的属性与(大多数)其他属性有点不同,因为它的存在足以禁用该元素。
<input type="text" name="test" disabled>
<input type="text" name="test" disabled="">
<input type="text" name="test" disabled="true">
<input type="text" name="test" disabled="false">
Those elements will all be disabled (yes, even the one with disabled="false"
) because the disabled attribute is present in the HTML. If you're seeing disabled=""
in Firebug's HTML tab after calling
这些元素都将被禁用(是的,即使是带有 的元素disabled="false"
),因为 disabled 属性存在于 HTML 中。如果您disabled=""
在调用后在 Firebug 的 HTML 选项卡中看到
.prop('disabled', true);
then that's the correct behaviour, and the element isdisabled. There's another reason why the values are still being submitted, despite being disabled.
那么这是正确的行为,并且该元素被禁用。尽管被禁用,但仍然提交值还有另一个原因。