如果选中复选框,则启用和禁用文本框 [英] Enable and disable textbox if checkbox is checked

查看:48
本文介绍了如果选中复选框,则启用和禁用文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了此

I read this article as suggested by an answer for a similar question. I did everything the article said, but the end result wasn't what I wanted.

我希望默认情况下禁用该文本框.选中该复选框后,将启用该文本​​框.

I want the text box to be disabled by default. When the checkbox is checked, the textbox is enabled.

发生的事情是默认情况下该文本框处于启用状态,并且选中该复选框时,该文本框处于禁用状态.

What happened was that the textbox is enabled by default, and when the checkbox is checked, the textbox is disabled.

这是我的代码:

<td class="trow2">
    {$prefixselect}<input type="text" class="textbox" name="subject" size="40" maxlength="85" value="{$subject}" tabindex="1" />
    <input type="checkbox" class="input_control"  value="subject" />
    <strong>I believe {$forum['name']} is the best section for this topic.</strong>
</td>

<script type="text/javascript">
    $(document).ready(function(){
        $('.input_control').attr('checked', true);
        $('.input_control').click(function(){
            if($('input[name='+ $(this).attr('value')+']').attr('disabled') == false) {
                $('input[name='+ $(this).attr('value')+']').attr('disabled', true);
            }
            else {
                $('input[name='+ $(this).attr('value')+']').attr('disabled', false);    
            }
        });
    });
</script>

推荐答案

您可以简化代码:

$(document).ready(function () {
    $('.input_control').change(function () {
        $('input[name=' + this.value + ']')[0].disabled = !this.checked;
    }).change();
});

演示: http://jsfiddle.net/t5qdvy9d/1/

这篇关于如果选中复选框,则启用和禁用文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆