Jquery 在页面加载时从列表中获取选定的单选按钮

发布于 2024-07-26 20:50:56 字数 87 浏览 7 评论 0原文

我想知道是否有人可以发布一个示例,说明如何在加载页面时通过 jquery 从 asp.net 单选按钮列表控件获取选定的单选按钮选项。

谢谢

I was wondering if anyone can post an example of how to get a selected radio button option from an asp.net radio button list control via jquery on the loading of a page.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼趣 2024-08-02 20:50:56

在您想要查询列表的 javascript 函数中,使用此代码..

var selected = jQuery('#<%= MyRadioButtonList.ClientID %> input:checked').val();
// or ...
var selected = $('#<%= MyRadioButtonList.ClientID %> input:checked').val();

使用所选单选按钮列表的结果设置示例标签,您可以这样做...

$(document).ready(function(){
    var selected = $('#<%= MyRadioButtonList.ClientID %> input:checked').val();
    $("#<%= MySampleLabel.ClientID %>").text(selected);
}

In your javascript function where you want to query the list, use this code..

var selected = jQuery('#<%= MyRadioButtonList.ClientID %> input:checked').val();
// or ...
var selected = $('#<%= MyRadioButtonList.ClientID %> input:checked').val();

to set a sample label with the results of your selected radiobuttonlist, you could do this...

$(document).ready(function(){
    var selected = $('#<%= MyRadioButtonList.ClientID %> input:checked').val();
    $("#<%= MySampleLabel.ClientID %>").text(selected);
}
不美如何 2024-08-02 20:50:56

工作示例此处

我用来获取单选按钮的选择器将抓取页面上所有具有 ofinterest 类的单选按钮。

$(function(){ 
  var value = $('input.ofinterest:checked').val();
  $('#result').text(value); 
});

如果您想进一步限制选择器的范围,并且不介意直接在 aspx/ascx 中编写 JS,则可以使用上面 Scott 的解决方案。 但是,如果您为您感兴趣的按钮提供已知的类名,则可以将此 JS 放入 .js 文件中。

Working example here.

The selector I used to get the radio buttons will grab all the radio buttons with the class ofinterest on the page.

$(function(){ 
  var value = $('input.ofinterest:checked').val();
  $('#result').text(value); 
});

If you want to scope the selector further, and you don't mind writing your JS directly in your aspx/ascx, you can use Scott's solution above instead. But if you give the buttons you're interested in a known classname, you can put this JS in a .js file.

十级心震 2024-08-02 20:50:56
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;
  }
}
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;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文