jquery - show textbox when checkbox checked

后端 未结 7 633
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 08:44

I have this form

相关标签:
7条回答
  • 2021-01-21 09:22

    Assuming markup will stay in same order can use next()

    jQuery(document).ready(function($) {
    
        $('input.maxtickets_enable_cb').change(function(){
                    $(this).next()[ this.checked ? 'show' : 'hide']();  
        }).change();
    });
    
    0 讨论(0)
  • 2021-01-21 09:22

    Put a div across your checkbox and text box

    <form action="">
    <div id="opwp_woo_tickets">
    <div>
        <input type="checkbox" class="maxtickets_enable_cb" name="opwp_wootickets[tickets][0][enable]">
        <div class="max_tickets">
            <input type="text" name="opwp_wootickets[tickets][0][maxtickets]">
        </div>
    </div>
    <div>
        <input type="checkbox" class="maxtickets_enable_cb" name="opwp_wootickets[tickets][1][enable]">
        <div class="max_tickets">
            <input type="text" name="opwp_wootickets[tickets][1][maxtickets]">
        </div>
    </div>
    <div>
        <input type="checkbox" class="maxtickets_enable_cb" name="opwp_wootickets[tickets][2][enable]">
        <div class="max_tickets">
            <input type="text" name="opwp_wootickets[tickets][2][maxtickets]">
        </div>
    </div>
    </div>
    </form>
    

    and replace your jquery code with this one below,

    jQuery(document).ready(function($) {
       $('input.maxtickets_enable_cb').change(function(){
           if ($(this).is(':checked')) $(this).parent().children('div.max_tickets').show();
           else $(this).parent().children('div.max_tickets').hide();
       }).change();
    });
    

    I have tested it and it works.

    0 讨论(0)
  • 2021-01-21 09:28
    protected void EnableTextBox()
    {
        int count = int.Parse(GridView1.Rows.Count.ToString());
    
        for (int i = 0; i < count; i++)
        {
            CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
            CheckBox cb1 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox2");
            CheckBox cb2 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox3");
            TextBox tb = (TextBox)GridView1.Rows[i].Cells[4].FindControl("txtration");
            TextBox tb1 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("txtjob");
            TextBox tb2 = (TextBox)GridView1.Rows[i].Cells[6].FindControl("txtaadhar");            
    
            if (cb.Checked == true)
            {
                tb.Visible = true;               
            }
            else
            {
                tb.Visible = false;    
            }
    
            if (cb1.Checked == true)
            {
                tb1.Visible = true;                
            }
            else
            {
                tb1.Visible = false;              
            }
    
            if (cb2.Checked == true)
            {
                tb2.Visible = true;               
            }
            else
            {
                tb2.Visible = false;
            }
        }
    }
    
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        EnableTextBox();
    }
    
    0 讨论(0)
  • 2021-01-21 09:30

    While you may need a JavaScript solution for other reasons, it's worth noting that this can be achieved with pure CSS:

    input + div.max_tickets {
        display: none;
    }
    
    input:checked + div.max_tickets {
        display: block;
    }
    

    JS Fiddle demo.

    Or, with jQuery, the simplest approach seems to be:

    // binds the change event-handler to all inputs of type="checkbox"
    $('input[type="checkbox"]').change(function(){
        /* finds the next element with the class 'max_tickets',
           shows the div if the checkbox is checked,
           hides it if the checkbox is not checked:
        */
        $(this).next('.max_tickets').toggle(this.checked);
    // triggers the change-event on page-load, to show/hide as appropriate:
    }).change();
    

    JS Fiddle demo.

    Reference:

    • CSS:
      • :checked pseudo-class.
    • jQuery:
      • change().
      • next().
      • toggle().
    0 讨论(0)
  • 2021-01-21 09:42

    As your dividers are placed next to your checkboxes, you simply need to use jQuery's next() method to select the correct elements:

    if ($(this).is(':checked'))
        $(this).next('div.max_tickets').show();
    else
        $(this).next('div.max_tickets').hide();
    

    Updated Codepen demo.

    From the documentation (linked above), the next() method selects:

    ...the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.

    Here we're selecting the next div.max_tickets element. However in your case just using next() with no parameters would suffice.

    0 讨论(0)
  • 2021-01-21 09:43

    Change:

    if ($(this).is(':checked')) $('div.max_tickets').show();
    

    To:

    if ($(this).is(':checked')) $(this).next('div.max_tickets').show();
    

    jsFiddle example here

    0 讨论(0)
提交回复
热议问题