1
A
回答
4
嘗試此
前端
function radioMe(e) {
if (!e) e = window.event;
var sender = e.target || e.srcElement;
if (sender.nodeName != 'INPUT') return;
var checker = sender;
var chkBox = document.getElementById('<%= chks.ClientID %>');
var chks = chkBox.getElementsByTagName('INPUT');
for (i = 0; i < chks.length; i++) {
if (chks[i] != checker)
chks[i].checked = false;
}
}
<asp:CheckBoxList runat="server" ID="chks">
<asp:ListItem>Item</asp:ListItem>
<asp:ListItem>Item</asp:ListItem>
<asp:ListItem>Item</asp:ListItem>
<asp:ListItem>Item</asp:ListItem>
</asp:CheckBoxList>
後端
protected void Page_Load(object sender, EventArgs e)
{
chks.Attributes.Add("onclick", "radioMe(event);");
}
但是在做這一切,你應該考慮RadioButtonList
控制
2
+0
要命。 – 2014-10-29 12:33:50
5
使用在RadioButtonList的一個的CheckBoxList可有時方便,因爲它允許用戶取消選擇的選擇,而不必給我發明一個解決方法。遵循瑜珈的回答,我想要一種方法,允許輕鬆重用,而不必將CheckBoxList的ClientID插入到每個用途的函數中。感謝模板瑜伽師。
用法:
<asp:CheckBoxList ID="m_testControl" runat="server" data-toggle="radio" RepeatDirection="Horizontal"> <asp:ListItem Value="CON">Concur</asp:ListItem> <asp:ListItem Value="CWI">Concur With Intent</asp:ListItem> <asp:ListItem Value="DNC">Do Not Concur</asp:ListItem> </asp:CheckBoxList>
JQuery的功能:
$(document).ready(function() { // turn all CheckBoxLists labeled for 'radio' to be single-select $('[data-toggle=radio]').each(function() { var clientId = $(this).attr('id'); $(this).find('input').each(function() { // set parent's id $(this).attr('data-parent', clientId); // add an onclick to each input $(this).click(function (e) { // ensure proper event if (!e) e = window.event; var sender = e.target || e.srcElement; if (sender.nodeName != 'INPUT') return; // toggle off siblings var id = $(this).attr('id'); var parent = $(this).attr('data-parent'); $('input[data-parent=' + parent + ']').not('#' + id).prop('checked', false); }); }); }); });
0
$('input[type=checkbox]').click(function() {
var chks = document.getElementById('<%= chkBranchRoleTransaction.ClientID %>').getElementsByTagName('INPUT');
for (i = 0; i < chks.length; i++) {
chks[i].checked = false;
}
$(this)[0].checked = true;
});
// or//
$('input[type=checkbox]').click(function() {
var chkBox = document.getElementById('<%= chkBranchRoleTransaction.ClientID %>');
var chks = chkBox.getElementsByTagName('INPUT');
for (i = 0; i < chks.length; i++) {
chks[i].checked = false;
}
$(this)[0].checked = true;
});
});[enter image description here][1]
[1]: http://i.stack.imgur.com/zzaaz.jpg
0
var id = "";
$("#CblProduct").on('click', ':checkbox', function() {
if (id != "")
{
$(id).attr('checked', false);
$(this).attr('checked', true);
}
if($("#CblProduct").length > 0)
{
id = this;
}
});
0
static int indexOfL=0;// the index of initial selected item
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
int i = 0;
foreach (ListItem li in CheckBoxList1.Items)
{
{
if (i != indexOfL && li.Selected)
{
indexOfL=i;
CheckBoxList1.ClearSelection();
li.Selected = true;
}
i++;
}
}}
我保存所選項目的索引,當有變化時,我們得到兩個選定的項目,所以在indexOfL的幫助下,我們得到了正確的項目。 我不「知道,如果這種方式是最好的,因爲這個代碼在服務器端運行.... 希望它可以幫助...
相關問題
- 1. 使用複選框選擇項目
- 2. 使用複選框選擇列表視圖項目
- 3. 如何選擇所有項目在選擇所有菜單項時選擇黑莓的列表框複選框
- 4. 如何使複選框列表只選擇一個選擇的項目
- 5. 選擇複選框列表選中的項目
- 6. 在樹狀複選框列表中選擇的子項目
- 7. 用單個複選框選擇和取消選中複選框列表
- 8. C#列表框組選擇的項目
- 9. C#列表框選擇項目
- 10. 從列表中選擇單個項目
- 11. 複選框列表選擇只有一個項目
- 12. 在列表框中選擇項目
- 13. 使列表框選擇選擇多個列表框c#
- 14. 在ASP .NET中使用複選框選擇列表視圖的項目
- 15. 試圖選擇複選框列表中的幾個選項
- 16. Copare功能和在複選框列表中選擇相同項目的項目
- 17. 如何在列表框行中選擇單個項目
- 18. 列表框未選擇所選項目
- 19. 從複選框列表中選擇一個複選框
- 20. 選擇列表框項目轉換成一個列表C#
- 21. 使用jquery從項目列表中選擇多個項目而不使用複選框
- 22. 簡單的列表項目多項選擇不選擇項目
- 23. 如何使用C中的單選按鈕選擇和取消選擇列表框中的所有項目#
- 24. 使用c#在wpf中選擇列表框中的多個項目#
- 25. 使用SharedPreferences保存複選框對話框中選擇項目
- 26. 檢查在Javascript中選擇了至少一個複選框列表項目
- 27. 從列表框中選擇項目XAML
- 28. 列表框中選擇所有項目
- 29. 獲得選擇從複選框列表中的項目使用jQuery
- 30. 選擇一個表單複選框
這豈不是更容易了一大堆只是使用單選按鈕? – Axxelsian 2012-07-27 12:05:13