简体   繁体   English

如何取消选中复选框上的文本框值?

[英]how to remove text box value on check box unchecked?

Hi i have two check boxes PlanA AND AndroidApps and one textbox when i checked check box then text box enable and when i unchecked thrn text dox disabled but problem is when tex box is enable and i write something in text box and then unchecked my checkbox then my textbox disabled but that text which i typed it does not delete and text box does not clear嗨,我有两个复选框 PlanA 和 AndroidApps 和一个文本框,当我选中复选框然后启用文本框时,当我取消选中 thrn text dox 禁用但问题是当启用 tex 框时,我在文本框中写了一些东西,然后取消选中我的复选框我的文本框被禁用,但我输入的文本不会删除,文本框也不会清除

Here is my code这是我的代码

<!DOCTYPE html>
<html>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('#main_box').on('change',function(e) {
        console.log("dfsd");
       $("#a2").prop("checked",this.checked);
     $("#emailid").prop("disabled", !$(this).is(':checked'));
    });
});
</script>
</head>

<body>

                <input id="main_box"type="checkbox" name="PlanA" value="A"><label for="PlanA"><span style="font-weight:bold">PlanA</span></label><br>
                <input name="PlanA" type="hidden" value=0 />


                <input type="checkbox" name="AndroidApps" id="a2" value=1><label for="AndroidApps"><span style="font-weight:bold">AndroidApps</span></label><br>
                <input name="AndroidApps" type="hidden" value=0 />


                         <input type="text" name="emailid"id="emailid"disabled="disabled" required size="45"  >

</body>
</html>

How can i achieve this我怎样才能做到这一点

Thanks in advance提前致谢

You can use val() to set the value of your input :您可以使用val()来设置input的值:

$("#emailid").val('');

Updated Fiddle更新的小提琴

Add a condition添加条件

$(document).ready(function () {
    $('#main_box').on('change', function (e) {
        $("#a2").prop("checked", this.checked);
        $("#emailid").prop("disabled", !this.checked);
        if(!this.checked){
            $("#emailid").val('');
        }
    });
});

Demo: Fiddle演示:小提琴

You can use val() to set the value of your input:您可以使用 val() 来设置输入的值:

$("#emailid").val('');

and if your are retrieving values, you use:如果您正在检索值,则使用:

$(selector).val()

Don't get confused with those two不要和这两个混淆

Hope this helps..希望这可以帮助..

Use this Link:: http://jsfiddle.net/rjha999/DM9b9/使用此链接:: http://jsfiddle.net/rjha999/DM9b9/

contains code for checkbox check ::包含复选框检查代码::

$("#chk").click(function(){
     if($("#chk").is(":checked"))
     {
        $("#txt").val('');
     }
})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM