【问题标题】:IE deselecting RadioButtonList after a javascript function returns falseIE 在 javascript 函数返回 false 后取消选择 RadioButtonList
【发布时间】:2011-08-03 22:09:46
【问题描述】:

我们有一个带有三个 ListItem 的 RadioButtonList。当用户进行选择时,我们希望显示一个确认框。如果他们点击取消,我们只想撤消选择更改。下面的代码在所有情况下都可以正常工作 - 但 IE 7、8 和 9。在 IE 中,在 return false 执行后没有选择任何 ListItem,但在所有其他情况下,只有下面的代码,选择被撤消,并且然后像我们想要的那样选择先前选择的项目。你没有工作吗?

标记:

<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" AutoPostBack="False">
    <asp:ListItem Selected="True" onClick="return confirmSubmit(this, event);">Value 1</asp:ListItem>
    <asp:ListItem onClick="return confirmSubmit(this, event);">Value 2</asp:ListItem>
    <asp:ListItem onClick="return confirmSubmit(this, event);">Value 3</asp:ListItem>
</asp:RadioButtonList>

Javascript:

function confirmSubmit(listItem, e) {
    var agree = confirm("Do you really want to change the value?");
    if (!agree) {
        return false;
    }
    else {
        document.forms[0].submit();
    }
}

【问题讨论】:

    标签: javascript asp.net internet-explorer


    【解决方案1】:

    最直接的跨浏览器兼容的处理方式是使用jQuery javascript 库。这是使用 jQuery 的解决方案,它适用于您的代码。

     <script type="text/javascript">
        var rblContainer;
        var lastChecked;
        $(document).ready(function () {
            rblContainer = $('#<%= RadioButtonList1.ClientID %>');
            lastChecked = rblContainer.children().find('input[checked]'); 
         });
        function confirmSubmit(listItem, e) {
            var l = $(listItem);
    
            var agree = confirm("Do you really want to change the recipient type?");
            if (!agree) {
                lastChecked.attr("checked", "checked");
                return false;
            }
            else {
                lastChecked = l;
                document.forms[0].submit();
            }
        }
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多