如何启用禁用的单选按钮?

发布于 2024-07-04 03:06:14 字数 1814 浏览 4 评论 0原文

以下代码在 IE 中运行良好,但在 FF 或 Safari 中不起作用。 我一辈子也无法弄清楚为什么。 如果您选择“禁用 2 个单选按钮”选项,该代码应该禁用单选按钮。 如果您选择“启用两个单选按钮”选项,它应该启用单选按钮。 这些都有效...

但是,如果您不使用鼠标在两个选项(“启用...”和“禁用...”)之间移动,则单选按钮似乎不会被正确禁用或启用,直到您单击页面上的其他任何位置(而不是单选按钮本身)。

如果有人有时间/好奇/感觉有帮助,请将下面的代码粘贴到 html 页面中并在浏览器中加载。 它在 IE 中工作得很好,但问题在 FF(我的例子中是 3)和 Safari 中表现出来,所有这些都在 Windows XP 上。

function SetLocationOptions() {
  var frmTemp = document.frm;
  var selTemp = frmTemp.user;
  if (selTemp.selectedIndex >= 0) {
    var myOpt = selTemp.options[selTemp.selectedIndex];
    if (myOpt.attributes[0].nodeValue == '1') {
      frmTemp.transfer_to[0].disabled = true;
      frmTemp.transfer_to[1].disabled = true;
      frmTemp.transfer_to[2].checked = true;
    } else {
      frmTemp.transfer_to[0].disabled = false;
      frmTemp.transfer_to[1].disabled = false;
    }
  }
}
<form name="frm" action="coopfunds_transfer_request.asp" method="post">
  <select name="user" onchange="javascript: SetLocationOptions()">
    <option value="" />Choose One
    <option value="58" user_is_tsm="0" />Enable both radio buttons
    <option value="157" user_is_tsm="1" />Disable 2 radio buttons
  </select>
  <br /><br />
  <input type="radio" name="transfer_to" value="fund_amount1" />Premium&nbsp;&nbsp;&nbsp;
  <input type="radio" name="transfer_to" value="fund_amount2" />Other&nbsp;&nbsp;&nbsp;
  <input type="radio" name="transfer_to" value="both" CHECKED />Both
  <br /><br />
  <input type="button" class="buttonStyle" value="Submit Request" />
</form>

The following code works great in IE, but not in FF or Safari. I can't for the life of me work out why. The code is supposed to disable radio buttons if you select the "Disable 2 radio buttons" option. It should enable the radio buttons if you select the "Enable both radio buttons" option. These both work...

However, if you don't use your mouse to move between the 2 options ("Enable..." and "Disable...") then the radio buttons do not appear to be disabled or enabled correctly, until you click anywhere else on the page (not on the radio buttons themselves).

If anyone has time/is curious/feeling helpful, please paste the code below into an html page and load it up in a browser. It works great in IE, but the problem manifests itself in FF (3 in my case) and Safari, all on Windows XP.

function SetLocationOptions() {
  var frmTemp = document.frm;
  var selTemp = frmTemp.user;
  if (selTemp.selectedIndex >= 0) {
    var myOpt = selTemp.options[selTemp.selectedIndex];
    if (myOpt.attributes[0].nodeValue == '1') {
      frmTemp.transfer_to[0].disabled = true;
      frmTemp.transfer_to[1].disabled = true;
      frmTemp.transfer_to[2].checked = true;
    } else {
      frmTemp.transfer_to[0].disabled = false;
      frmTemp.transfer_to[1].disabled = false;
    }
  }
}
<form name="frm" action="coopfunds_transfer_request.asp" method="post">
  <select name="user" onchange="javascript: SetLocationOptions()">
    <option value="" />Choose One
    <option value="58" user_is_tsm="0" />Enable both radio buttons
    <option value="157" user_is_tsm="1" />Disable 2 radio buttons
  </select>
  <br /><br />
  <input type="radio" name="transfer_to" value="fund_amount1" />Premium   
  <input type="radio" name="transfer_to" value="fund_amount2" />Other   
  <input type="radio" name="transfer_to" value="both" CHECKED />Both
  <br /><br />
  <input type="button" class="buttonStyle" value="Submit Request" />
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀中猫帐中妖 2024-07-11 03:06:14

嗯,IE 有一个有点不标准的对象模型; 你所做的事情不应该起作用,但你却逃脱了惩罚,因为 IE 对你很好。 在 Firefox 和 Safari 中,代码中的 document.frm 计算结果为 undefined。

您需要在表单元素上使用 id 值,并使用 document.getElementById('whatever') 返回对它们的引用,而不是引用文档对象不存在的属性。

因此,这会更好一些,并且可能会满足您的需求:

Line 27: <form name="frm" id="f" ...

Line 6: var frmTemp = document.getElementById('f');

但是,如果您想了解更多有关处理事情的正确方法的信息,您可能想看看这本优秀的书: DOM 脚本 作者:Jeremy Keith

另外,当我们讨论这个主题时,同一作者的 Bulletproof Ajax 也值得在您的书架上占有一席之地,JavaScript:好的部分 作者:Doug Crockford

Well, IE has a somewhat non-standard object model; what you're doing shouldn't work but you're getting away with it because IE is being nice to you. In Firefox and Safari, document.frm in your code evaluates to undefined.

You need to be using id values on your form elements and use document.getElementById('whatever') to return a reference to them instead of referring to non-existent properties of the document object.

So this works a bit better and may do what you're after:

Line 27: <form name="frm" id="f" ...

Line 6: var frmTemp = document.getElementById('f');

But you might want to check out this excellent book if you want to learn more about the right way of going about things: DOM Scripting by Jeremy Keith

Also while we're on the subject, Bulletproof Ajax by the same author is also deserving of a place on your bookshelf as is JavaScript: The Good Parts by Doug Crockford

醉生梦死 2024-07-11 03:06:14

为什么不使用 AJAX 脚本库之一,它们抽象了许多跨浏览器 DOM 脚本黑魔法,让生活变得更加轻松。

Why not grab one of the AJAX scripting libraries, they abstract away a lot of the cross browser DOM scripting black magic and make life a hell of a lot easier.

拥醉 2024-07-11 03:06:14

要让 FF 在使用键盘时模仿 IE 的行为,可以在选择框上使用 keyup 事件。 在您的示例中(我不喜欢以这种方式附加事件处理程序,但这是另一个主题),它会是这样的:

<select name="user" id="selUser" onchange="javascript:SetLocationOptions()" onkeyup="javascript:SetLocationOptions()">

To get FF to mimic IE's behavior when using the keyboard, you can use the keyup event on the select box. In your example (I am not a fan of attaching event handlers this way, but that's another topic), it would be like this:

<select name="user" id="selUser" onchange="javascript:SetLocationOptions()" onkeyup="javascript:SetLocationOptions()">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文