使用复选框清除表单 [英] Clear form using a checkbox
问题描述
我正尝试使用复选框清除表单,但我不希望重置清除复选框本身。我使用的代码如下:
< input name =no_cageid =no_cagetype =复选框value =1onclick =this.form.reset();>
当我点击复选框时,它会清除表单,但不幸的是, p>
复选框还会更新一个数据库,如果选中,则为1,如果未选中,则为0。所以我需要它的形式,它需要能够切换之间选中和未选中。
有两种方法做到这一点:
创建一个javascript函数重置表单,然后再次选中复选框为true。
使用JQuery :
< script src =https://ajax.googleapis.com/ AJAX /库/ jquery的/ 2.1.1 / jquery.min.js>< /脚本>
< script type =text / javascript> $()。$($)$($)$($)$($)$ );
this.checked = true;
}
});
< / script>
< form id =client>
< input type =text>
< input id =no_cagetype =checkboxvalue =1> CLEAR
< / form>
使用Javascript :
< script type =text / javascript>
函数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_cagetype =checkboxonclick =javascript:clearform();> CLEAR
< / form>
OR,
保留复选框不在表单中
< script type =text / javascript>
函数clearform(){
document.getElementById(client)。reset();
}
< / script>
< form id =client>
< input type =text>
< / form>
< input id =no_cagetype =checkboxvalue =1onclick =javascript:clearform();> CLEAR
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. 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.
Using 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:
<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
这篇关于使用复选框清除表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!