开发者

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. 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. 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. Also you have an extra </tr> tag in there.

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.

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).

Note: This will only work in the aspx/ascx file, not in a external JavaScript file.


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. 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜