0

CheckBoxListHTMLテーブルを作成するダイナミクスがあります。簡単にするために、1つの行をリストしました。

<table id="checkBoxList1" border="0">
    <tr>
        <td><span someValue="neededValue"><input id="checkBoxList1_0" type="checkbox" name="checkBoxList1$0" checked="checked" /><label for="checkBoxList1_0">TestTopic1</label></span></td>
    </tr>
</table>

クリックするとJavaScriptを実行するASPボタンもあります。を返す関数が必要"neededValue"です。ASPボタンとjavascript関数は次のとおりです。

<asp:Button ID="btnSubscribe" runat="server" Text="Update Subscriptions" OnClientClick="return GetSelectedItem()"/>


<script type="text/javascript">
function GetSelectedItem() {
    var CHK = document.getElementById("<%=checkBoxList1.ClientID%>");
    var checkbox = CHK.getElementsByTagName("input");
    var spans = CHK.getElementsByTagName("span");
    for (var i = 0; i < checkbox.length; i++) {
        if (checkbox[i].checked) {
            alert("Selected = " + spans[i]);
        }
    }
    return false;
}
</script>

現在、null値を返します。

4

1 に答える 1

1

に置き換えspans[i]ますcheckbox[i].parentNode.getAttribute('someValue')

jQueryの使用をお勧めします。たとえば、次のように、コードが大幅に簡素化されます。

<asp:Button ID="btnSubscribe" runat="server" Text="Update Subscriptions" />

$(function() {
    $('#<%=btnSubscribe.ClientID%>').click(function (e) {
        $('#<%=checkBoxList1.ClientID%> input:checked').each(function() {
            alert('Selected = ' + $(this).closest('span').attr('someValue'));
        });
        e.preventDefault();
    });
});
于 2012-12-28T19:27:11.097 に答える