Jquery没有返回checkbox的值。但返回复选框的ID。怎么样?

时间:2013-05-23 16:44:25

标签: javascript jquery asp.net .net

我使用以下脚本显示所选的ID复选框。哪个工作得很好。当试图获得价值时,它会给我返回值。

<div id="selected-ID"></div>   

     function storeID() {
            var ID = $checkboxes.map(function () {
                if (this.checked) return this.id;    //this.value;
            }).get().join(',');
            $('#selected-ID').html(ID);
        }

        $(function () {
            $checkboxes = $('input:checkbox').change(storeID);
        });

问题可能是因为,我的所有复选框都在树视图中,树的子节点中复选框的呈现HTML看起来像这样。

如何克服这个问题,并将ID存储在变量中?请稍微说清楚。

为树视图渲染的HTML:

<div style="font-size: 11px; font-family: Tahoma; font-weight: bold; text-align: left;" onclick="OnTreeClick(event)" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index">
        <table cellspacing="0" cellpadding="0" style="border-width:0;">
            <tbody><tr>
                <td><a href="javascript:__doPostBack('ctl00$cphMain$ctlEsnSearchByServices$ctl05$Tree_Index','tfc052442-3b20-4cdc-8d20-9c8544ac1c6f')"><img style="border-width:0;" alt="Collapse Other Services" src="/WebResource.axd?d=_533LbcrYzxclETTqT9E9lbjpoP9iiSVLn9WP908dD96sH9D2JXH4BN8Q2OdATM4TYJU67kfDSnJQpffp_ITMVu3Y839pir-IzEhB41mqu6U8EWb0&amp;t=634604712479085897"></a></td><td class="toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_2"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn0CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn0CheckBox"><a style="border-style:none;font-size:1em;" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indext0" href="javascript:__doPostBack('ctl00$cphMain$ctlEsnSearchByServices$ctl05$Tree_Index','sfc052442-3b20-4cdc-8d20-9c8544ac1c6f')" class="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_0 toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_1">Other Services</a></td>
            </tr>
        </tbody></table><table cellspacing="0" cellpadding="0" style="border-width:0;">
            <tbody><tr>
                <td><div style="width:20px;height:1px"></div></td><td><img alt="" src="/WebResource.axd?d=Ptpr_s5NKQd2NhXQH_MiSClxDVR5ZHar9QZwFenhB3L_8-vIXRA_NHlxqP3iWPQWzSbDizVZ5YO8o3q4fX1XUhismQEMBdpf8AuHvaSyrvYco4WB0&amp;t=634604712479085897"></td><td class="toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_2"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn1CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn1CheckBox"><a style="border-style:none;font-size:1em;" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indext1" href="javascript:__doPostBack('ctl00$cphMain$ctlEsnSearchByServices$ctl05$Tree_Index','sfc052442-3b20-4cdc-8d20-9c8544ac1c6f\\45bb0a98-ec44-e211-beaf-005056ba0039')" class="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_0 toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_1">Mobile Services</a></td>
            </tr>
        </tbody></table><table cellspacing="0" cellpadding="0" style="border-width:0;">
            <tbody><tr>
                <td><div style="width:20px;height:1px"></div></td><td><img alt="" src="/WebResource.axd?d=Ptpr_s5NKQd2NhXQH_MiSClxDVR5ZHar9QZwFenhB3L_8-vIXRA_NHlxqP3iWPQWzSbDizVZ5YO8o3q4fX1XUhismQEMBdpf8AuHvaSyrvYco4WB0&amp;t=634604712479085897"></td><td class="toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_2"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn2CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn2CheckBox"><a style="border-style:none;font-size:1em;" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indext2" href="javascript:__doPostBack('ctl00$cphMain$ctlEsnSearchByServices$ctl05$Tree_Index','sfc052442-3b20-4cdc-8d20-9c8544ac1c6f\\9b3dafdf-217e-e111-9ca9-005056ba0039')" class="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_0 toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_1">Hospice Day Care</a></td>
            </tr>
        </tbody></table><table cellspacing="0" cellpadding="0" style="border-width:0;">
            <tbody><tr>
                <td><div style="width:20px;height:1px"></div></td><td><img alt="" src="/WebResource.axd?d=Ptpr_s5NKQd2NhXQH_MiSClxDVR5ZHar9QZwFenhB3L_8-vIXRA_NHlxqP3iWPQWzSbDizVZ5YO8o3q4fX1XUhismQEMBdpf8AuHvaSyrvYco4WB0&amp;t=634604712479085897"></td><td class="toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_2"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn3CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn3CheckBox"><a style="border-style:none;font-size:1em;" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indext3" href="javascript:__doPostBack('ctl00$cphMain$ctlEsnSearchByServices$ctl05$Tree_Index','sfc052442-3b20-4cdc-8d20-9c8544ac1c6f\\f83dafdf-217e-e111-9ca9-005056ba0039')" class="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_0 toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_1">Website - Disability Information Website</a></td>
            </tr>
        </tbody></table><table cellspacing="0" cellpadding="0" style="border-width:0;">
            <tbody><tr>
                <td><div style="width:20px;height:1px"></div></td><td><img alt="" src="/WebResource.axd?d=Ptpr_s5NKQd2NhXQH_MiSClxDVR5ZHar9QZwFenhB3L_8-vIXRA_NHlxqP3iWPQWzSbDizVZ5YO8o3q4fX1XUhismQEMBdpf8AuHvaSyrvYco4WB0&amp;t=634604712479085897"></td><td class="toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_2"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn4CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn4CheckBox"><a style="border-style:none;font-size:1em;" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indext4" href="javascript:__doPostBack('ctl00$cphMain$ctlEsnSearchByServices$ctl05$Tree_Index','sfc052442-3b20-4cdc-8d20-9c8544ac1c6f\\33333333-217e-e111-9ca9-005056ba0039')" class="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_0 toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_1">Information &amp; Referral Services</a></td>
            </tr>
        </tbody></table><table cellspacing="0" cellpadding="0" style="border-width:0;">
            <tbody><tr>
                <td><div style="width:20px;height:1px"></div></td><td><img alt="" src="/WebResource.axd?d=BOvsSO2B3BKMupOSRxbma89ER-i_r1FrqfnVsgVq_QiVog3IJUebdD29i4BVAo26-lbJh_BLXmsQGU_VArXhNt6ejiWJk87whzKer6ts-_kM9Vt90&amp;t=634604712479085897"></td><td class="toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_2"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn5CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn5CheckBox"><a style="border-style:none;font-size:1em;" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indext5" href="javascript:__doPostBack('ctl00$cphMain$ctlEsnSearchByServices$ctl05$Tree_Index','sfc052442-3b20-4cdc-8d20-9c8544ac1c6f\\44444444-217e-e111-9ca9-005056ba0039')" class="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_0 toggle ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Index_1">Ambulance Service (Non-Emergency)</a></td>
            </tr>
        </tbody></table>
    </div>

1 个答案:

答案 0 :(得分:0)

复选框中没有值 你应该在输入中添加value属性,而在另一个问题上,输入标签没有关闭

<input type="checkbox" value="Other Services" id="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn0CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_ctl05_Tree_Indexn0CheckBox" />