【问题标题】:IE can't use "return false" on radio buttonIE 不能在单选按钮上使用“return false”
【发布时间】:2015-09-16 22:26:27
【问题描述】:

我使用下面的 js 来防止点击某些单选按钮。

它在 Chrome 中完全可以正常工作。

但在 IE8 上仍然无法检查,但单击组中的其他收音机时默认值消失了。

 $("body").on("change", ".readonly", function () {
    return false;
});

我怎样才能使收音机只读并且也可以在 IE8 上工作?

【问题讨论】:

  • 属性“disabled”不是做你想做的事吗?
  • 我仍然希望将数据提交到服务器。如果我使用禁用隐藏字段。我必须编辑很多页面。所以我尝试另辟蹊径。

标签: javascript html internet-explorer-8 radio-button


【解决方案1】:

<input type="radio" readonly /> 不起作用,但使用 click 可以部分起作用。如果您有两个同名的收音机,IE 仍会取消选中已选中的收音机,无论是否使用此代码

$("body").on("click", ".readonly", function(e) {
  e.preventDefault();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input name="rad" type="radio" class="readonly" />
<input name="rad" type="radio" checked class="readonly" />

这里有一个解决方案:https://stackoverflow.com/a/5437904/295783

$(function() {
  $(':radio:not(:checked)').attr('disabled', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input name="rad" type="radio" class="readonly" />
<input name="rad" type="radio" checked class="readonly" />

【讨论】:

  • 如果您有两台收音机并且其中一台已经检查过,则它不起作用。选中的将取消选中。
  • 感谢您的解决方案。禁用不检查的工作真的很好。
猜你喜欢
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多