2017-05-11 16 views
0

これは私のコードのDatalistです。JavaScriptがaspで動作していません:データリストチェックボックスonchange

<asp:DataList runat="server" ID="dlstate" RepeatColumns="6"> 
    <ItemTemplate> 
     <asp:CheckBox runat="server" ID="chk" Text='<%#Eval("area_state") %>' OnCheckedChanged="chk_state_SelectedIndexChanged" onchange="return checkconfirm(this);" AutoPostBack="true" /> 
    </ItemTemplate> 
</asp:DataList> 

これはJavascriptコードです。

<script type="text/javascript"> 
    function checkconfirm(a) { 
     if (a.checked == true) { 
      return true; 
     } 
     else { 
      debugger; 
      var box = confirm("Are you sure you want to unselect this state as areas alloted under this state will be lost?"); 
      if (box == true) 
       return true; 
      else { 
       a.checked = true; 
       return false; 
      } 
     } 
    } 
</script> 

問題は、このapsxページがHTMLでレンダリングされるときonchange="return checkconfirm(this);"スパンではなく、チェックボックスの上に表示されていることです。私は確認のためにいくつかの方法を試しましたが、私はonchangeイベントをチェックボックスに入れることができません。

+0

「Onchange」で「onchange」を変更しようとします。 – Webruster

答えて

1

これにはjQueryリスナーを使用できます。

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#<%= dlstate.ClientID %> input[type='checkbox']").change(function() { 
      if (!$(this).prop("checked")) { 
       var box = confirm("Are you sure you want to unselect this state as areas alloted under this state will be lost?"); 
       if (box == true) 
        return true; 
       else { 
        $(this).prop("checked", "checked"); 
        return false; 
       } 
      } 
     }); 
    }); 
</script> 
関連する問題