【问题标题】:get ASPX radiobutton text or value from javascript从 javascript 获取 ASPX 单选按钮文本或值
【发布时间】:2013-02-06 11:36:57
【问题描述】:

ASPX 单选按钮位于数据列表模板中

<asp:RadioButton ID="RadioButton1" runat="server"  
        GroupName="rdb" 
        Text='<%#  Eval("type1") %>'  
        onclick="Getr1(this)"/>

JavaScript

function getr1(val){
    alert(val)
}

如果我在任何 html 控件中使用此函数,它将返回一个值,但它在 aspx 单选按钮中不起作用。我使用此代码:onchange="GetQty(this.options[this.selectedIndex].value)" 从 aspx 下拉列表中获取选定的索引,它工作得很好,也许有人可以帮助我找出 aspx rb 的正确语法。我已经尝试过onclick="Getr1(this.options[this.checked].value)",在此先感谢

【问题讨论】:

  • 你试过OnClientClick吗?
  • 将问题标题更改为已解决不是这里的方法。请标记为答案,帮助您解决问题的那个。

标签: javascript asp.net radio-button


【解决方案1】:

您的代码很好,只需要稍作改动。

<asp:RadioButton ID="RadioButton1" runat="server"  
    CliientIDMode="Static" GroupName="rdb" 
    Text='<%#  Eval("type1") %>' />

 <asp:Label ID="Label1" runat="server" style="font-size: x-small"
    Text='<%# Eval("type1") %>'></asp:Label>

用于获取RadioButton Text的jQuery:

$("#RadioButton1").click(function{
    alert( $(this).siblings('label').text());
});

用于在 RadioButton 旁边获取 asp:Label Text 的 jQuery:

$("#RadioButton1").click(function{
    alert( $(this).siblings('span').text());
});

希望对你有帮助:)

【讨论】:

  • 感谢您的回复,但我仍然无法获得价值
  • ok 这里是更新的javascript函数GetR1(obj) { if (obj.checked = true) { alert(obj.value);返回 id:RadioButton) 这里是单选按钮: 及其标签:
  • 现在我知道该功能正在运行,但我怎样才能获得收音机或标签的文本。如果我尝试 alert(obj.text) 返回识别。我只需要修改js,但此时我一无所知。感谢您花时间帮助我。我真的很感激。
  • 我也试过 GetR1(obj) { if (obj.checked = true){var labelText = document.getElementById("Label1").value or text alert(labelText)} 它返回 {objectHTMLInputElement}
  • @CarlosSiles : 你想要单选按钮文本还是 asp:label 文本?
【解决方案2】:

试试这个 JavaScript。

function Getr1(elm) {
    var nextElm = elm.nextSibling;
    //catching white space, comments etc...
    while (nextElm && nextElm.nodeType != 1) {
        nextElm = nextElm.nextSibling
    }
    alert(nextElm.innerHTML);
}

原因:asp:RadioButton在浏览器中是这样渲染的

<input id="MainContent_RadioButton1" type="radio" onclick="Getr1(this);" 
     value="RadioButton1" name="ctl00$MainContent$rdb">
<label for="MainContent_RadioButton1">your rendered text here</label>

如果你使用jQuery,它会像这样简单。

function Getr1(elm) {
    alert( $(elm).next().html() );
}

【讨论】:

  • 你好 Navee,我试过 onclientClick 但还是不行,有什么方法可以改变你的 javascript 为 aspx 单选按钮?谢谢
  • 我没有要求你使用 OnClientClick 只是在你的 asp 中写 put onclick:RadioButton 就像这样 onclick="Getr1(this)"
  • Naveen,还有一个问题:我可以在选中时验证复选框,但在未选中时如何验证?我需要设置 extra='NO' 这是我的 js 代码: function extra() { var elem=document.getElementById("Checkbox2"); if (elem.checked = true) { extra = "YES";}} 提前致谢
猜你喜欢
  • 2013-03-19
  • 2012-01-04
  • 1970-01-01
  • 2016-10-11
  • 1970-01-01
  • 2012-11-08
  • 2018-11-25
  • 1970-01-01
相关资源
最近更新 更多