Javascript 中的无线电输入和警报

发布于 2024-11-05 07:01:20 字数 400 浏览 5 评论 0原文

我需要一些帮助。 我有一个程序可以根据单选框中的用户输入更改表值 类似于此页面:

clicky

但问题是我希望用户选择无线电输入仅一次;如果他们选择另一个 那么表的值就会变得混乱。 所以我想知道的是,当用户选择无线电输入两次时如何制作警报框?

与此网站类似clicky尝试单击单选按钮两次并弹出警报。

请问有人可以帮忙吗?

I need some help.
I have program that changes table values based on user input in radio box
similar to this page:

clicky

But the problem is I want users to select the radio input only once; if they select another
radio then values of tables get messed up.
So what I want to know is how can I make an alert box when user selects the radio input twice?

Similar to this website clicky try clicking radio button twice and alert popsup.

Please can anyone help?

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

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

发布评论

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

评论(2

°如果伤别离去 2024-11-12 07:01:20

如果不引入外部库的帮助,很难阻止单选按钮输入节点上的事件。一个简单的解决方案是仅禁用附加到每个输入节点的 onclick 函数中的按钮。像这样: http://jsfiddle.net/c73Mh/ 。如果他们出于某种原因仍然需要能够选择单选按钮,您可以通过选择最初从同一功能中选择的按钮来取消选择。希望这有帮助!

It's difficult to prevent an event on a radio button input node without bringing in help from outside libraries. A simple solution is to just disable the buttons from within an onclick function attached to each input node. Like so: http://jsfiddle.net/c73Mh/ . If they still need to be able to select the radio buttons for whatever reason, you can cancel the selection by selecting the button that was initially selected from within that same function. Hope this helps!

×纯※雪 2024-11-12 07:01:20

为了简单起见,在我的示例中,我假设每个单选按钮的 id 是名称和值属性的组合。除了我在下面给您提供的内容之外,您还需要添加某种类型的 Reset() 函数来设置 selectedValues = {}; 并取消选择所有单选按钮。

<input type="radio" name="group1" value="A" id="group1A" onclick="radioClicked(this);" />First option
<input type="radio" name="group1" value="B" id="group1B" onclick="radioClicked(this);" />Second option

<input type="radio" name="group2" value="A" id="group2A" onclick="radioClicked(this);" />First option
<input type="radio" name="group2" value="B" id="group2B" onclick="radioClicked(this);" />Second option

var selectedValues = {};

function radioClicked(rb) {
  if (selectedValues[rb.name] === undefined) {
    selectedValues[rb.name] = rb.value;
    doTableProcessing();
  }
  else {
    alert("You can't change the selected values");
    document.getElementById(rb.name + selectedValues[rb.name]).checked = true;
  }
}

For simplicity in my example I'm assuming that the id of each radio button is a combination of the name and value attributes. In addition to what I've given you below you will need to add a reset() function of some kind that sets selectedValues = {}; and deselects all radio buttons.

<input type="radio" name="group1" value="A" id="group1A" onclick="radioClicked(this);" />First option
<input type="radio" name="group1" value="B" id="group1B" onclick="radioClicked(this);" />Second option

<input type="radio" name="group2" value="A" id="group2A" onclick="radioClicked(this);" />First option
<input type="radio" name="group2" value="B" id="group2B" onclick="radioClicked(this);" />Second option

var selectedValues = {};

function radioClicked(rb) {
  if (selectedValues[rb.name] === undefined) {
    selectedValues[rb.name] = rb.value;
    doTableProcessing();
  }
  else {
    alert("You can't change the selected values");
    document.getElementById(rb.name + selectedValues[rb.name]).checked = true;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文