简体   繁体   English

如果嵌套的ASP标签的值如何获取? (客户端)

[英]How do I acquire the value of a ASP label if it is nested? (client-side)

So there is a ASP checkboxlist that gets rendered to the table and in the code behind the checkboxes get bound with a label from a code table. 因此,有一个ASP复选框列表呈现到表中,并且复选框后面的代码中绑定了代码表中的标签。 I need to display a text box only if a certain checkbox is checked off. 仅当选中某个复选框时,才需要显示一个文本框。

<table id="chkSomeCheckbox">
    <tbody>
       <tr>
     <td>
          <input type="checkbox" id="chkSomeCheckbox_0">
          <label for="chkSomeCheckbox_0">Acme</label>
         </td>
         <td>
          <input type="checkbox" id="chkSomeCheckbox_1">
          <label for="chkSomeCheckbox_1">Other</label>
         </td>
       </tr>
    </tr>
   </tbody>
 </table>

The checkboxlist renders a table which the element retains the ID. 复选框列表将呈现一个表,其中该元素保留了ID。 I have tried to just get the value of the label when a checkbox is checked and then go from there to create my logic - but I can't seem to be able to grab the value of the label. 我试图仅在选中一个复选框时获取标签的值,然后从那里创建我的逻辑-但我似乎无法获取标签的值。 I have tried the following different ways: 我尝试了以下几种方法:

$("#<%chkSomeCheckbox.ClientID> input").next("label").val();

or 要么

$("#<%chkSomeCheckbox.ClientID> input").next().val();

I am using the input selector because when I get this first part working, I will need to do some logic on it. 我正在使用输入选择器,因为当我使第一部分工作时,我将需要对它进行一些逻辑处理。

Any thoughts? 有什么想法吗?

This is tagged as ASP.NET rather than ASP so I'm curious as to why you are not using ASP.NET controls. 这被标记为ASP.NET而不是ASP,因此我很好奇为什么不使用ASP.NET控件。 Also you have an extra </tr> tag in there. 另外,您还有一个</tr>标记。

You could do this: 您可以这样做:

<table id="chkSomeCheckbox">
    <tr>
        <td>
            <asp:CheckBox ID="chkSomeCheckbox_0" runat="server" 
             Text="Acme" Checked="true" />
        </td>
        <td>
            <asp:CheckBox ID="chkSomeCheckbox_1" runat="server" 
             Text="Other" Checked="false" />
        </td>
    </tr>
</table>

In the Page_Load event of the code behind you could then check the Checked property of the CheckBox and then decide to display the area that you held the Textbox in or just change the Visible property of the TextBox itself. 在后面的代码的Page_Load事件中,您可以检查CheckBoxChecked属性,然后决定显示保留Textbox的区域,或者只是更改TextBox本身的Visible属性。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (chkSomeCheckbox_0.Checked)
        {
            txtSomeTextbox.Visible = true;
        }
    }
}

Tried this? 试过这个?

$("#<%= chkSomeCheckbox.ClientID %> input").next("label").val();

In your code sample the ASP.NET inline statement is not closed (and also the "=" sign is missing). 在您的代码示例中,ASP.NET内联语句未关闭(并且缺少“ =”符号)。

Note: This will only work in the aspx/ascx file, not in a external JavaScript file. 注意:这仅适用于aspx / ascx文件,不适用于外部JavaScript文件。

If you can grab a handle on the label that you want, you can probably call the .text() method to get the text of the label. 如果可以在所需的标签上抓住一个句柄,则可以调用.text()方法以获取标签的文本。 As such, I don't think that labels have a "value" 因此,我认为标签没有“值”

For example, if you wanted to append a textbox after the label based on which one was checked you could do this: 例如,如果您想在标签后附加一个文本框,则可以根据以下内容进行检查:

$("#chkSomeCheckbox > input").change(function(){
    $label = $(this).next();
    if($(this).attr("selected") == true)
    {
        //get label text
        foo = $label.text();

        //append textbox after the label, etc
        $label.append(textbox);
    }

});A

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 WCF 创建客户端库 - How do I create a client-side library for WCF 如何在没有客户端事件的情况下从ASP.NET代码后面调用jQuery UI对话框? - How do I call up a jQuery UI dialog from ASP.NET code behind without a client-side event? 如何在ASP.NET Core 2.0中的自定义验证属性中进行客户端验证? - How To Do Client-Side Validation In Custom Validation Attribute In ASP.NET Core 2.0? 如何在ASP.NET Core 1.0中删除Bower并将npm用于客户端? - How can I drop bower and use npm for client-side in ASP.NET Core 1.0? 如何在ASP.NET MVC项目中为knockout生成客户端视图模型? - How can I generate client-side view models for knockout in an ASP.NET MVC project? 在C#中,如何对客户端用户交互和javascript(jQuery)代码进行单元测试? - In C#, how do I unit-test client-side user interaction and javascript (jQuery) code? 如何使用包含点 (.) 的字符串路由参数在客户端 blazor 中进行路由? - How do I route in client-side blazor with a string route parameter containing dots (.)? 如何在 Blazor 客户端应用程序的服务中调用 HttpClient - How Do I Call HttpClient in Service for a Blazor Client-Side App 如何在 Blazor 中执行客户端 UI 事件 - How to do client-side UI events in Blazor 需要在回发时访问客户端标签值(Angular指令)…获取{{radioModel || 每次&#39;null&#39;}} - Need to access client-side label value (Angular directive) on postback…getting {{radioModel || 'null'}} every time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM