简体   繁体   中英

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. 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.

Unfortunately, IE is office standard so I'm stuck with it. 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.

The 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:

<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. Apparently IE considers selecting the radio button to be its default action. So remove this from the handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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