jQuery - 当复选框被选中时显示文本框。

3
我有这个表单。
<form action="">
  <div id="opwp_woo_tickets">
    <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>

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

    <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>
</form>

目前,我正在使用这个 jQuery 代码来在复选框被选中时显示文本框。

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

它可以正常工作,但选中时会显示所有文本框。

有人能帮我解决这个问题吗?

这是我的问题演示。

http://codepen.io/mistergiri/pen/spBhD

7个回答

8

由于您的分隔线紧挨着复选框,因此您只需要使用 jQuery 的 next() 方法 来选择正确的元素:

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

更新的 Codepen 演示

从文档(上面链接中)中可以看到,next() 方法选择:

...匹配元素集合中每个元素之后的紧邻兄弟元素。如果提供了选择器,则仅检索下一个同级元素(与选择器匹配的元素)。

在这里,我们选择了下一个 div.max_tickets 元素。然而,在您的情况下,只使用没有参数的 next() 即可。


3
假设标记将保持相同的顺序,可以使用next()
jQuery(document).ready(function($) {

    $('input.maxtickets_enable_cb').change(function(){
                $(this).next()[ this.checked ? 'show' : 'hide']();  
    }).change();
});

1

也许只尝试选择下一个元素?

更改:

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

to:

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

1

更改:

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

收件人:

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

在这里查看jsFiddle示例


1

虽然出于其他原因你可能需要使用JavaScript解决方案,但值得注意的是这也可以通过纯CSS实现:

input + div.max_tickets {
    display: none;
}

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

JS Fiddle演示

或者,使用jQuery,最简单的方法似乎是:

// 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演示

参考资料:


1
在你的复选框和文本框之间放置一个div。
<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>

并用下面的代码替换您的jQuery代码:
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();
});

我已经测试过它,它能够正常工作。

-1
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();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接