如何从插入innerHtml 的单选按钮获取值

发布于 2024-08-12 07:56:21 字数 302 浏览 5 评论 0原文

我有一个带有单选按钮列的表格。我设法使单选按钮列动态插入到单元格中(div 如果重要)。但是,在回发时,innerHtml 尚未使用“checked”属性进行更新。

您能否告诉我如何(在服务器上)找到单选按钮是否已被选中?

更多信息:这是更新面板内的用户控制。

将是一个很好的帖子我的主题,仍然没有帮助

I have sort of a table with a radio-button column. I managed to make radio-button column work dynamically inserting into a cell (div if matter). But, on postback innerHtml hasn't been updated with "checked" attribute.

Could you give me an idea how can I find out (on the server) if radio-button has been checked?

More info: This is on user control inside update panel.

This would be good post on my topic, still doesn't help

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

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

发布评论

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

评论(4

黄昏下泛黄的笔记 2024-08-19 07:56:22

出于任何原因,您不能使用标准 asp:RadioButton 并使用 javascript 来确保它是互斥的。我之前已经通过向单选按钮添加自定义属性,然后使用 js 函数取消选中具有该属性的所有项目,然后检查选定的项目来完成此操作。这解决了 IE 问题,该问题阻止组名属性在不同容器中的单选框上工作。

radioButton.InputAttributes.Add("ClientGroupName", "grpRadioList");
radioButton.InputAttributes.Add("onclick",
  string.Format(
     "javascript:radiobuttonToggle('{0}','ClientGroupName','grpRadioList');"  
      ,radioButton.ClientID));

并使用以下 JS 取消选中所有无线电,然后选中您想要的无线电。
请注意,我使用了 InputAttributes 而不是 Attributes,因为单选按钮包含在 span 标记内,因此 InputAttributes 用于添加到实际输入控件而不是 span 的项目。

function radiobuttonToggle(selectedRB, attribName, attribValue)
{
    var objRadio = document.getElementById(selectedRB);

    for(i = 0; i < document.forms[0].elements.length; i++)
    {
        elm = document.forms[0].elements[i];
        if (elm.type == 'radio')
        {
            if(elm.getAttribute(attribName) == attribValue)
                elm.checked = false;
        }
    }
    objRadio.checked = true; 
}

然后,您可以将 radioButton.Checked 公开为 CS 文件中的属性,并将其重新用作控件。

Any reason you cannot use a standard asp:RadioButton and use javascript to ensure it is mutually exclusive. I have done this before by adding a custom attribute to the radiobutton and then using a js function to uncheck all items with that attribute and then check the selected one. This works around the IE issue which prevents the groupname attribute from working on radioboxes that are in different containers.

radioButton.InputAttributes.Add("ClientGroupName", "grpRadioList");
radioButton.InputAttributes.Add("onclick",
  string.Format(
     "javascript:radiobuttonToggle('{0}','ClientGroupName','grpRadioList');"  
      ,radioButton.ClientID));

and use the following JS to uncheck all radios and then check the one you want.
Note i used InputAttributes instead of Attributes as the radiobutton is wrapped inside a span tag so InputAttributes is for items added to the actual input control rather than the span.

function radiobuttonToggle(selectedRB, attribName, attribValue)
{
    var objRadio = document.getElementById(selectedRB);

    for(i = 0; i < document.forms[0].elements.length; i++)
    {
        elm = document.forms[0].elements[i];
        if (elm.type == 'radio')
        {
            if(elm.getAttribute(attribName) == attribValue)
                elm.checked = false;
        }
    }
    objRadio.checked = true; 
}

You can then expose radioButton.Checked as a property in your CS file and reuse this as a control.

你是年少的欢喜 2024-08-19 07:56:22

检查 Form.Request("radio-name") != null

只有在检查后您才会得到一个非空值。

Check Form.Request("radio-name") != null

You only get a non-null value when it's been checked.

晨与橙与城 2024-08-19 07:56:22

确保您的页面元素在回发时正确重建。第一次插入单选按钮的任何绑定过程都必须重新运行,然后才能第二次访问它们。

Make sure your page elements are being rebuilt correctly on postback. Any binding process that inserted the radio buttons the first time around will have to be re-run before you can access them the second time.

幻想少年梦 2024-08-19 07:56:22

这是一个工作示例,首先我通过方法 you linked

function addRadio()
{
   try{  
        rdo = document.createElement('<input type="radio" name="fldID" />');  
    }catch(err){  
     rdo = document.createElement('input');  
    }  
    rdo.setAttribute('type','radio');  
    rdo.setAttribute('name','fldID'); 
    document.getElementById('container').appendChild(rdo);
}

然后在后面的代码中,我仅使用下面的代码来获取单选按钮的值:

string value = Request["fldID"];

因此,请确保您正在尝试获取服务器端单选按钮的名称。您应该在服务器端使用 name 属性,而不是 id。

Here is a working example, first I add radios to my webform by the method you linked :

function addRadio()
{
   try{  
        rdo = document.createElement('<input type="radio" name="fldID" />');  
    }catch(err){  
     rdo = document.createElement('input');  
    }  
    rdo.setAttribute('type','radio');  
    rdo.setAttribute('name','fldID'); 
    document.getElementById('container').appendChild(rdo);
}

Then at code behind I used only the code below to get the radio's value :

string value = Request["fldID"];

So, be sure you're trying to get the name of the radio buttons at server side. You should use name attribute at server side, not id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文