Tôi đã tự hỏi nếu có ai có thể đăng một ví dụ về cách để có được một lựa chọn nút radio chọn từ một danh sách nút radio asp.net kiểm soát thông qua jquery trên tải của một trang.Jquery được chọn nút radio từ danh sách khi tải trang
Cảm ơn
Tôi đã tự hỏi nếu có ai có thể đăng một ví dụ về cách để có được một lựa chọn nút radio chọn từ một danh sách nút radio asp.net kiểm soát thông qua jquery trên tải của một trang.Jquery được chọn nút radio từ danh sách khi tải trang
Cảm ơn
Trong chức năng javascript của bạn nơi bạn muốn truy vấn danh sách, sử dụng mã này ..
var selected = jQuery('#<%= MyRadioButtonList.ClientID %> input:checked').val();
// or ...
var selected = $('#<%= MyRadioButtonList.ClientID %> input:checked').val();
để thiết lập một nhãn mẫu với kết quả của RadioButtonList chọn của bạn, bạn có thể làm điều này ...
$(document).ready(function(){
var selected = $('#<%= MyRadioButtonList.ClientID %> input:checked').val();
$("#<%= MySampleLabel.ClientID %>").text(selected);
}
Ví dụ làm việc here.
Bộ chọn tôi đã sử dụng để nhận các nút radio sẽ lấy tất cả các nút radio có lớp ofinterest
trên trang.
$(function(){
var value = $('input.ofinterest:checked').val();
$('#result').text(value);
});
Nếu bạn muốn phạm vi bộ chọn thêm và không ngại viết JS trực tiếp vào aspx/ascx, bạn có thể sử dụng giải pháp của Scott ở trên thay thế. Nhưng nếu bạn cung cấp cho các nút bạn quan tâm đến một tên lớp đã biết, bạn có thể đặt JS này trong một tệp .js.
protected void radioButton_CheckedChanged(object sender, EventArgs e)
{
throw new ApplicationException("Radio Changed");
RadioButton rb = (RadioButton)sender;
TextBox tbexact = (TextBox)this.UpdatePanel1.FindControl("TextBoxExact");
TextBox tbpartial = (TextBox)this.UpdatePanel1.FindControl("TextBoxPartial");
DropDownList dropdown = (DropDownList)this.UpdatePanel1.FindControl("DropDownListCountries");
RadioButton rbc = (RadioButton)this.UpdatePanel1.FindControl("RadioButtonExact");
if (tbexact == null)
throw new ApplicationException("Could not find control");
else
throw new ApplicationException("Found it");
if (rbc != null && rb.Equals(rbc))
{
tbpartial.Enabled = false;
dropdown.Enabled = false;
mCriteria = SearchCriteria.Exact;
}
rbc = (RadioButton)this.UpdatePanel1.FindControl("RadioButtonPartial");
if (rbc != null && rb.Equals(rbc))
{
tbexact.Enabled = false;
dropdown.Enabled = false;
mCriteria = SearchCriteria.Partial;
}
rbc = (RadioButton)this.UpdatePanel1.FindControl("RadioButtonPerCountry");
if (rbc != null && rb.Equals(rbc))
{
tbexact.Enabled = false;
tbpartial.Enabled = false;
mCriteria = SearchCriteria.Country;
}
}
... giả định rằng SearchOptions là điều khiển trong đó các nút radio được hiển thị. –
chính xác - ví dụ được cập nhật để cố gắng làm rõ. –
Cảm ơn scott. Điều này hoạt động hoàn hảo. – zSynopsis