简体   繁体   English

检查Firefox中是否选中了单选按钮

[英]Checking if radio buttons are checked in Firefox

On my site, I have two checkboxes created in my ASP.NET MVC view like so: 在我的网站上,我在ASP.NET MVC视图中创建了两个复选框,如下所示:

Html.RadioButton("check", "true", "true" == (string) ViewData["someKey"], new { id = "check1"});
Html.RadioButton("check", "false", "false" == (string) ViewData["someKey"], new { id = "check2"});

I am positive that ViewData["someKey"] has the value "true" in it. 我肯定ViewData [“ someKey”]的值是“ true”。

In my JS init function, I perform the following check: 在我的JS init函数中,执行以下检查:

alert($('#check1').is(':checked') + " " + $('#check2').is(':checked'));

In Firefox (and only Firefox), my alert dialog will show the following (it works as expected in every other browser): 在Firefox(和仅Firefox)中,我的警报对话框将显示以下内容(它在所有其他浏览器中均能正常工作):

Initial page load: true false
Normal refresh via Ctrl + R: false false
Refresh skipping cache via Ctrl + Shift + R: true false

I have tried many different methods of looking at the checkbox value, including $('#check1').attr('checked') without success. 我尝试了许多查看复选框值的方法,包括$('#check1').attr('checked')均未成功。 If I examine the HTML in Firebug, I can see that the first radio button has the property checked="checked" in it. 如果检查Firebug中的HTML,可以看到第一个单选按钮的属性checked="checked"

Why is the checkbox value changing in FF when I refresh, and how can I mitigate this? 为什么刷新时FF中的复选框值会发生变化,该如何缓解呢? Since this seems to be a FF-only bug, how can I change my code to make it work? 由于这似乎是仅FF的错误,如何更改代码以使其正常工作?

This SO question seemed to ask something similar, but none of the proposed solutions seem to work in this case. 这个SO问题似乎提出了类似的问题,但是在这种情况下,所有提议的解决方案似乎都不起作用。

Edit : I should also point out that when the radio button is rendered after the refresh in FF, it's not actually being displayed as checked either, despite what the HTML is telling me. 编辑 :我还应该指出,当在FF中刷新后呈现单选按钮时,尽管HTML告诉了我,它实际上也没有显示为选中状态。

Edit2 : Adding raw HTML as per request Edit2 :根据请求添加原始HTML

<input id="check1" type="radio" value="True" name="check" checked="checked"/>
<input id="check2" type="radio" value="False" name="check"/>

Can we see the generated HTML? 我们可以看到生成的HTML吗? Are you really creating two radio buttons with both the same name and the same value? 您是否真的在创建两个具有相同名称相同值的单选按钮?

Remember when you hit refresh, browsers try to preserve the form field contents of the page before the refresh. 请记住,当您单击刷新时,浏览器会尝试在刷新之前保留页面的表单字段内容。 This includes radio button state. 这包括单选按钮状态。 If you have two indistinguishable radio buttons that would seem to be a good way to confuse a browser trying to re-establish the form state. 如果您有两个无法区分的单选按钮,那么这似乎是使浏览器尝试重新建立表单状态的一种好方法。

It sounds like your script could be running before the page is fully loaded. 听起来您的脚本可能在页面完全加载之前正在运行。 Where did you put the Javascript code? 您将Javascript代码放在哪里? If you haven't already, try moving the code to the end of the <body> block. 如果还没有,请尝试将代码移到<body>块的末尾。

I had the same problem a few days ago and the issue was that I was doing the "check" before the item was fully loaded or rendered, it was a DropDownList that I rendered with jQuery. 几天前我遇到了同样的问题,问题是我在项目完全加载或呈现之前进行了“检查”,这是我使用jQuery呈现的DropDownList。

My case was very specific, but I had to rework the code to take this into account... 我的案例非常具体,但是我不得不重新编写代码以考虑到这一点...

It seems that the problem isn't with checking whether or not the radio buttons are checked, but more that they buttons are actually unchecked even though the HTML code says otherwise. 看来问题不在于检查单选按钮是否被选中,而是更多的是,即使HTML代码另有说明,它们实际上也未被选中。

I've posted a followup question to this one here as an addendum to this topic. 我已经发布了后续问题这一个在这里作为附录这个话题。

EDIT : This page has a good description of the problem and the solutions you can take to solve it. 编辑此页面对问题以及您可以用来解决的解决方案进行了很好的描述。

We've had a similar bug in firefox when using jquery to change the selection. 使用jquery更改选择时,我们在firefox中遇到了类似的错误。 Even though the code changed correctly according to firebug none of the radio buttons was selected. 即使代码已根据Firebug正确更改,也未选择任何单选按钮。 The solution we found is to remove the radio buttons from the DOM and just put them back. 我们发现的解决方案是从DOM中删除单选按钮,然后将其放回原处。

var allradiobuttons = $('...');
allradiobuttons.click(function() {
  var radiobutton = $(this);
  allradiobuttons.removeAttr("checked");

  radiobutton.attr("checked", "checked");
  var parent = radiobutton.parent();
  parent.html(parent.html()); // This is a weird fix
}

I used this to force firefox to reset the radiobuttons checked upon refresh. 我用它来迫使Firefox重新设置刷新时选中的单选按钮。

$(function(){ $('input[name="radiobuttongroup"]')[0].checked = true; });

where [0] is the index of the radiobutton in the specific group ( [0] = first radio button, [1] = second radio button, ..) 其中[0]是特定组中单选按钮的索引([0] =第一个单选按钮,[1] =第二个单选按钮,..)

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

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