简体   繁体   English

如何启用/禁用单选按钮交互的文本字段?

[英]How to enable/disable text field on radio button interaction?

Cannot get this to work.无法使其正常工作。 First time using variables passed into functions.第一次使用传递给函数的变量。 Unchecking radio button should disable form field and vice versa.取消选中单选按钮应禁用表单字段,反之亦然。 lineid variable distinguishes this radio/text input pair from 10 others. lineid 变量将此单选/文本输入对与其他 10 个输入对区分开来。

My code:我的代码:

<script type="text/javascript">
function disablefield(lineid){ 
if (document.getElementById(lineid).checked == true){ 
document.dupedit.lineid.disabled = false;
} else { 
document.dupedit.lineid.disabled = true;
} 
}
</script>

Subset of my HTML.我的 HTML 的子集。

You need to pass a string into your disablefield function, so put the value in quotes when you pass it in. Something like:您需要将一个字符串传递到您的disablefield ,因此在传递时将值放在引号中。类似于:

<input onclick="disablefield('2671997')" />

This is because document.getElementById expects a string, not an integer.这是因为document.getElementById需要一个字符串,而不是 integer。

Secondly, to enable/disable the field, you need to use disabled = true;其次,要启用/禁用该字段,您需要使用disabled = true; rather than = 'disabled' .而不是= 'disabled'

document.dupedit.lineid is looking a for a field with name "lineid", which doesn't exist in your form. document.dupedit.lineid正在查找名称为“lineid”的字段,该字段在您的表单中不存在。 I would suggest giving the field an id and using document.getElementById again instead.我建议给该字段一个id并再次使用document.getElementById

If you want to continue using the name attribute, you will have to use document.getElementsByName instead.如果要继续使用name属性,则必须改用document.getElementsByName This returns an array of matching elements (since multiple elements can share the same name), but if in your code you know that the element in question is the only one with that name, you can do this:这将返回一个匹配元素的数组(因为多个元素可以共享相同的名称),但是如果在您的代码中您知道所讨论的元素是唯一具有该名称的元素,您可以这样做:

document.getElementsByName(lineid)[0].disabled = true;

You can see a working version (I think this is how you wanted it anyway) here .你可以在这里看到一个工作版本(我认为这就是你想要的)。 And here is a version using getElementsByName .是使用getElementsByName的版本。

You are missing a closing brace on the function:您在 function 上缺少右括号:

function disablefield(lineid){ 
if (document.getElementById(lineid).checked == true){ 
document.dupedit.lineid='enabled';
}else{ 
document.dupedit.lineid='disabled';
} 
} //<-- here

Also, can I suggest you pass this to the function.另外,我可以建议您将this传递给 function。 Then you don't have to call getElementById然后你不必调用getElementById

<input onclick='disablefield(this)' type.....

function disablefield(obj){ 
    if (obj.checked == true){ 
        document.dupedit.lineid='enabled';
    }else{ 
        document.dupedit.lineid='disabled';
    } 
} 

I think what you need is to re-think the code.我认为您需要重新考虑代码。

  • Don't use ID on the checkbox.不要在复选框上使用 ID。 Better move that ID to the text field you want to disable/enable and check whether that field is disabled/enabled, not the checkbox itself最好将该 ID 移动到您要禁用/启用的文本字段并检查该字段是否已禁用/启用,而不是复选框本身

  • use cleaner JS.使用更清洁的 JS。

Please, take a look at the jsFiddle , I have compiled for you.请看一下我为你编译的 jsFiddle Does it do what you expect, Dan?它符合你的预期吗,丹?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM