简体   繁体   English

使用复选框清除表单

[英]Clear form using a checkbox

I am trying to use a checkbox to clear a form but I do not want the reset to clear the checkbox itself. 我试图使用复选框清除表单,但我不希望重置清除复选框本身。 The code I am using is as follows: 我使用的代码如下:

<input name="no_cage" id="no_cage" type="checkbox" value="1" onclick="this.form.reset();">

When I click on the checkbox it clears the form fine but unfortunately, also clear itself too. 当我单击复选框时,它可以很好地清除表单,但不幸的是,也可以清除自身。

The checkbox also updates a db with a 1 if checked or a 0 if unchecked. 如果选中该复选框,则该数据库还将更新为1;如果未选中,则该数据库将更新为0。 So I need it within the form and it needs to be able to toggle between checked and unchecked. 因此,我需要在表单中使用它,并且它需要能够在选中和未选中之间切换。

There are two ways to do this: 有两种方法可以做到这一点:

Create a javascript function to reset the form and then check the checkbox again to true. 创建一个javascript函数以重置表单,然后再次选中该复选框为true。

Using JQuery : 使用JQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).on('change', '#no_cage', function() {
    if(this.checked) {
        document.getElementById("client").reset();
        this.checked = true;
    }
});
</script>
<form id="client">
    <input type="text">
    <input id="no_cage" type="checkbox" value="1"> CLEAR
</form>

Using Javascript : 使用Javascript

<script type="text/javascript">
function clearform(){
    if (document.getElementById("no_cage").checked == true){
        document.getElementById("client").reset();
        document.getElementById("no_cage").checked = true;
    }
}
</script>
<form id="client">
    <input type="text">
    <input id="no_cage" type="checkbox" onclick="javascript:clearform();"> CLEAR
</form>

OR, keep the checkbox out of the form 或者,将复选框保留在表格之外

<script type="text/javascript">
function clearform(){
    document.getElementById("client").reset();
}
</script>
<form id="client">
    <input type="text">
</form>
<input id="no_cage" type="checkbox" value="1" onclick="javascript:clearform();"> CLEAR

I don't know if you want it this way, but I change the code that if checkbox is checked than clear form else it won't. 我不知道您是否需要这种方式,但是我更改了以下代码:如果选中了复选框,则不会清除格式,否则不会。 Here the code 这里的代码

<form id ="myForm" name = "test">
<input type ="text"/>
<input type ="text"/>
<input type ="text"/>
<input type ="text"/>
</form>


<input name="no_cage" id="no_cage" type="checkbox"  onclick="resetForm()">

<script type="text/javascript">

function resetForm()

{
if (document.getElementById("no_cage").checked == true)

{ 

document.getElementById("myForm").reset();

}

}
</script>
<form id="myForm">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br><br>
<input type="checkbox"  id="box"onclick="myFunction()" value="Reset form">reset
</form>
<script>
function myFunction() {
document.getElementById("myForm").reset();
document.getElementById("box").checked = true;
}</script>

hope it helps 希望能帮助到你

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

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