I must sincerely thank Darren Davies for his help with the following code from which I have made my test html file.
我必须真诚地感谢Darren Davies对我编写测试html文件的以下代码的帮助。
<html>
<head>
<title>field_enable</title>
<script type="text/javascript" src="script/jquery.js"></script>
<script>
$().ready(function() {
$('#clicker').click(function() {
$('input').each(function() {
if ($(this).attr('disabled')) {
$(this).removeAttr('disabled');
}
else {
$(this).attr({
'disabled': 'disabled'
});
}
});
});
});
</script>
</head>
<body>
<div id='clicker' style='background-color:#FF0000; height:20px; width:50px;'></div>
<br>
<input type='text' disabled></input>
<input type='text' disabled></input>
<input type='text' disabled></input>
</body>
</html>
This works perfectly however my form has a large number if fields within which I need to disable only certain one for various clients. When I apply the code it most certainly activates the fields that I have disabled however it also disables the rest of the fields in the form even though they have no attribute set. My problem now it to work out how to activate only the fields that I have disabled and leave the rest enabled.
这完全适用,但是如果我需要为各种客户端禁用某个字段的字段,我的表单有很多。当我应用代码时,它肯定会激活我已禁用的字段,但它也会禁用表单中的其余字段,即使它们没有设置属性。我现在的问题是弄清楚如何仅激活我已禁用的字段并启用其余字段。
2 个解决方案
#1
If you only want to affect certain fields you could assign those fields a particular class (changeable in this example)
如果您只想影响某些字段,可以将这些字段分配给特定的类(在此示例中可更改)
<input type='text' disabled class="changeable" />
and change this line
并改变这一行
$('input').each(function() {
to
$('input.changeable').each(function() {
so that it only works on inputs with the given class
所以它只适用于给定类的输入
#2
It's disabling the other fields because your "if" is telling it to do so. When you use:
它禁用了其他字段,因为你的“if”告诉它这样做。当你使用:
if ($(this).attr('disabled'))
when the input doesn't has this attr, it fails the condition so it will disable the input! :)
当输入没有此attr时,它会使条件失败,因此它将禁用输入! :)
What you can do to check is:
你可以做的检查是:
if ($(this).is("[disabled]") && $(this).attr('disabled')){
// do your stuff here
}
So using elem.is("attr-name") you can know if it has your attr.
所以使用elem.is(“attr-name”)你可以知道它是否有你的attr。
Another solution was pointed out by Ivan, you can use classes to diference your inputs.
Ivan指出了另一种解决方案,你可以使用类来改变你的输入。
Cheers
#1
If you only want to affect certain fields you could assign those fields a particular class (changeable in this example)
如果您只想影响某些字段,可以将这些字段分配给特定的类(在此示例中可更改)
<input type='text' disabled class="changeable" />
and change this line
并改变这一行
$('input').each(function() {
to
$('input.changeable').each(function() {
so that it only works on inputs with the given class
所以它只适用于给定类的输入
#2
It's disabling the other fields because your "if" is telling it to do so. When you use:
它禁用了其他字段,因为你的“if”告诉它这样做。当你使用:
if ($(this).attr('disabled'))
when the input doesn't has this attr, it fails the condition so it will disable the input! :)
当输入没有此attr时,它会使条件失败,因此它将禁用输入! :)
What you can do to check is:
你可以做的检查是:
if ($(this).is("[disabled]") && $(this).attr('disabled')){
// do your stuff here
}
So using elem.is("attr-name") you can know if it has your attr.
所以使用elem.is(“attr-name”)你可以知道它是否有你的attr。
Another solution was pointed out by Ivan, you can use classes to diference your inputs.
Ivan指出了另一种解决方案,你可以使用类来改变你的输入。
Cheers