2011-08-03 13 views
0

3つのListItemを持つRadioButtonListがあります。ユーザーが選択すると、確認ボックスが表示されます。彼らがキャンセルを押すと、選択の変更を取り消すだけです。以下のコードは、IE 7,8,9のすべてでうまくいきます。IEでは、戻り値falseが実行された後でListItemsが選択されませんが、他のコードでは、選択が取り消され、私たちが望むように、以前に選択されたアイテムが選択されます。 Y U NO NO WORK IE?javascript関数がfalseを返すと、IEはRadioButtonListの選択を解除します

マークアップ:

<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(); 
    } 
} 

答えて

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> 
関連する問題