简体   繁体   English

Internet Explorer:选择按钮后,jQuery取消选择单选按钮

[英]Internet Explorer: Jquery unselects radio button after button is selected

I have listener for a set of radio buttons (radio group). 我有一组单选按钮(单选组)的侦听器。 Using IE and a radiobutton is selected, it makes a database call to populate a select. 使用IE并选择一个单选按钮,它将进行数据库调用以填充选择。 However, when a radio button is selected, the code runs fine but then it unselects the selected radio button. 但是,当选择单选按钮时,代码可以正常运行,但随后它取消选择所选的单选按钮。 Doesn't happen on Firefox or Chrome. 在Firefox或Chrome上不会发生。

Unfortunately, IE is office standard so I'm stuck with it. 不幸的是,IE是办公标准,因此我坚持使用它。 I can fix the problem by added javascript code to re-select the radio button, but I'd like to know why it does it in first place. 我可以通过添加JavaScript代码来重新选择单选按钮来解决此问题,但我想知道为什么它会首先这样做。

The jquery: jQuery的:

$('body').on("change", "input[type=radio]", function () {
    var orgId = $('input[name=rdoGroup1]:checked').val();
    $.get('AJAXServlet', {formType: 'getSearchList', type: orgId}, function (responseJson) {
        var $select = $('#selSearch');
        $select.find('option').remove();
        $.each(responseJson, function (key, value) {
            $('<option>').val(key).text(value).appendTo($select);
        });
    });
    return false;
});

The HTML: HTML:

<fieldset>
    <legend style="color: lightblue;">&nbsp;Search&nbsp;&nbsp;</legend>
    <input type="radio" id="organization" name="rdoGroup1" value="organization" checked/>Organization&nbsp;&nbsp;&nbsp;
    <input type="radio" id="department" name="rdoGroup1" value="department" />Department&nbsp;&nbsp;&nbsp;
    <input type="radio" id="office" name="rdoGroup1" value="office" />Office&nbsp;&nbsp;&nbsp;
    <input type="radio" id="rdoName" name="rdoGroup1" value="name" />Name
    <br><br>
    <select id="selSearch" name="selSearch" value="" style="width: 250px;">
        <option value="1">LAW ENFORCEMENT</option>
        <option value="2">FACILITIES & MAINTENANCE</option>
        <option value="3">PUBLIC WORKS</option>
        <option value="4">SECURITY</option>
        <option value="5">EXTERNAL AGENCIES</option>
    </select>
    <button onclick="doSearch()">Search</button>
    <br><br>
</fieldset>

return false at the end of an event handler prevents the default action of the event. 在事件处理程序的末尾return false阻止事件的默认操作。 Apparently IE considers selecting the radio button to be its default action. 显然,IE认为选择单选按钮是其默认操作。 So remove this from the handler. 因此,从处理程序中删除它。

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

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