取消选中复选框时清除特定的表单元素 [英] Clear specific form elements when checkbox is unchecked

查看:81
本文介绍了取消选中复选框时清除特定的表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个基本的示例,说明取消选中复选框时如何清除特定的表单输入(每个表单都有其自己的唯一ID)?我已经尝试了几种解决方案,但似乎并不能使它可靠地工作.我需要仅在未选中此框时才发生这种情况,这似乎给我带来了问题.

Can anyone give me a basic example of how to clear specific form inputs (each with it's own unique ID) when a checkbox is unchecked? I've tried a handful of solutions but just can't quite seem to get this working reliably. I need this to only happen when the box is unchecked, which seems to be giving me issues.

推荐答案

一个大概的例子是...

A rough example might be...

<input type='checkbox' id='clear_text' />
<input type='text' id='text_box' value='Contents' />
<script>
$(function(){
    $("#clear_text").click(function(){
        if(!$(this).is(":checked")){ //checks if the checkbox/this is selected or not
            $("#text_box").val(""); //empty the input value
        } else { } //nothing
    });
});
</script>

这篇关于取消选中复选框时清除特定的表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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