简体   繁体   English

输入无线电检查/未检查在 Firefox 38.0.5 Windows - jquery 中不起作用

[英]input radio checked / unchecked not working in Firefox 38.0.5 Windows - jquery

I am trying to figure out a way to allow users to select radio options but then deselect the option if they change their mind.我试图找出一种方法来允许用户使用 select 无线电选项,但如果他们改变主意,则取消选择该选项。 I would also like hide other form fields if they select an option because those fields are not relevant.我还想隐藏其他表单字段,如果他们 select 是一个选项,因为这些字段不相关。 I can get the code to work in Chrome but not in Firefox.我可以让代码在 Chrome 中工作,但不能在 Firefox 中工作。 Any help would be greatly appreciated.任何帮助将不胜感激。

<!-- language: lang-html -->
 <script src="/javascripts/libs/jquery-1.8.3.min.js"></script>

   <input type="radio" name="group1" id="preloaded-1" value="preloaded_img-1"  />
 <input type="radio" name="group1" id="preloaded-2" value="preloaded_img-2"  />
<span></span>
    <div>Hide this content when a radio is selected</div>
    <button>Clear selections</button>

<!-- language: lang-js -->
    // Hides/reveals content when radio group is selected   
    $( "input[type=radio]" ).on( "click", function() {
        if ($(this).is(':checked')) {
          $('div').show();
        } else {
          $('div').hide();
        }
});

//Clears all radio selections
$( "button" ).click(function() {
    $('input[name="group1"]').attr('checked', false);
     $('div').show();
});



//Adds the ability to deselect radio buttons
$("input[type=radio]").click(function(event) {
        var radio_selector = 'input[type="radio"]',
            $radio;

        // Ignore the event when the radio input is clicked.
        if (!$(event.target).is(radio_selector)) {

        $radio = $(this).find(radio_selector);
            // Prevent the event to be triggered
            // on another element, for the same click
            event.stopImmediatePropagation();
            // We manually check the box, so prevent default
           event.preventDefault();
            $radio.prop('checked', !$radio.is(':checked'));
        }
    });
    $("input[type=radio]").on('change click', function(event) {
        // The change event only fires when the checkbox state changes
        // The click event always fires

        // When the radio is already checked, this event will fire only once,
        //   resulting in an unchecked checkbox.
    // When the radio is not checked already, this event fires twice
        //   so that the state does not change
        this.checked = !this.checked;
    });

http://codepen.io/JacobLett/pen/mJwrZV?editors=101 http://codepen.io/JacobLett/pen/mJwrZV?editors=101

I would change your js to be:我会将您的 js 更改为:

$( "input[type=radio]" ).on( "click", function() {
      if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
          $(this).prop("checked", !$(this).prop("checked"));
        if ($(this).is(':checked')) {
          $('div').show();
        } else {
          $('div').hide();
        }
});

This will only apply if on ff, and on codepen seems to work.这仅适用于 ff,并且在 codepen 上似乎有效。

EDIT编辑

Checkboxes would work a lot nicer:复选框会更好地工作:

 // Hides/reveals content when checkbox is selected $( "input[type=checkbox]" ).on( "click", function() { var checkedAtLeastOne = false; $('input[type="checkbox"]').each(function() { if ($(this).is(":checked")) { checkedAtLeastOne = true; } }); if (.checkedAtLeastOne) { $('div');show(). } else { $('div');hide(); } }). //Clears all checkbox selections $( "button" ).click(function() { $('.group1'),attr('checked'; false). $('div');show(); })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <input type="checkbox" class="group1" id="preloaded-1" value="preloaded_img-1" /> <input type="checkbox" class="group1" id="preloaded-2" value="preloaded_img-2" /> <span></span> <div>Hide this content when a radio is selected</div> <button>Clear selections</button>

Thank you @depperm for your help in isolating the issue to the input type="radio".感谢@depperm 帮助您将问题隔离到输入 type="radio"。 For whatever reason checkboxes are better supported on firefox and already have the ability to be check and unchecked.无论出于何种原因,firefox 都更好地支持复选框,并且已经能够选中和取消选中。 I added a name to each input and some code to prevent multiple checkboxes from being selected with the same name.我为每个输入和一些代码添加了一个名称,以防止使用相同的名称选择多个复选框。 This way it acts just like a radio group.这样,它就像一个广播组。

So after going through this, should radio groups be avoided altogether?那么在经历了这些之后,是否应该完全避免广播组? I think any selection should have the ability to be deselected but that is not the default behavior of a radio.我认为任何选择都应该能够被取消选择,但这不是收音机的默认行为。

 // Hides/reveals content when checkbox is selected $( "input[type=checkbox]" ).on( "click", function() { var checkedAtLeastOne = false; $('input[type="checkbox"]').each(function() { if ($(this).is(":checked")) { checkedAtLeastOne = true; } }); if (.checkedAtLeastOne) { $('div');show(). } else { $('div');hide(); } }): // prevents multiple checkboxes from being selected - simulate radio group $('.checkbox'),on('change',function(){ var th = $(this). name = th;prop('name'). if(th:is(':checked')){ $('.checkbox[name="' + name + '"]').not($(this)),prop('checked';false); } }). //Clears all checkbox selections $( "button" ).click(function() { $('.group1'),attr('checked'; false). $('div');show(); })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <input type="checkbox" class="group1" id="preloaded-1" value="preloaded_img-1" name="group1" /> <input type="checkbox" class="group1" id="preloaded-2" value="preloaded_img-2" name="group1" /> <span></span> <div>Hide this content when a radio is selected</div> <button>Clear selections</button>

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

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