英文:
when using a <input> checkbox i can not use javascript to uncheck the checkbox
问题
以下是您要翻译的内容:
"when making a simple form for a small project to make a fake photography page i was atempting to add a checkbox form that can be unchecked when you press the submit button after reaserch on outher posts i was unable to find a fix for my problem any help?
<div>
<fieldset>
<legend>Appointment scheduling</legend>
<p>Your Name: <p>
<textarea name="story" rows="1" cols="50" id='output1'> </textarea>
<p>Your Email: <p>
<textarea name="story" rows="1" cols="50" id='output2'> </textarea>
<p>date:</p>
<input type="date" id="start" name="trip-start" value="2023-06-17" min="2018-01-01" max="2035-12-31">
<br>
<div>
<input type="checkbox" id="checkbox" >
<label>Regular photoshoot</label>
</div>
<div>
<input type="checkbox" id="checkbox" >
<label>Nature/Outdoors</label>
</div>
<div>
<input type="checkbox" id="checkbox" >
<label>Wedding</label>
</div>
<div>
<input type="checkbox" id="checkbox" >
<label>senior photoshoot</label>
</div>
<div>
<input type="checkbox" id="checkbox" >
<label>Family photoshoot</label>
</div>
<div>
<input type="checkbox" id="checkbox">
<label>Pets/Animals</label>
</div>
<button class="button button1" type="button" onclick="javascript:eraseText();">Submit</button>
</fieldset>
</div>
<script>
function eraseText() {
document.getElementById("output1").value = "";
document.getElementById("output2").value = "";
const checkbox = document.getElementById("checkbox");
checkbox.removeAttribute('checked');
}
</script>
use of jquery as seen on this page https://stackoverflow.com/questions/13557623/remove-attribute-checked-of-checkbox did not change the function and did nothing to the code"
英文:
when making a simple form for a small project to make a fake photography page i was atempting to add a checkbox form that can be unchecked when you press the submit button after reaserch on outher posts i was unable to find a fix for my problem any help?
<div>
<fieldset>
<legend>Appointment scheduling</legend>
<p>Your Name: <p>
<textarea name="story" rows="1" cols="50" id='output1'> </textarea>
<p>Your Email: <p>
<textarea name="story" rows="1" cols="50" id='output2'> </textarea>
<p>date:</p>
<input type="date" id="start" name="trip-start" value="2023-06-17" min="2018-01-01" max="2035-12-31">
<br>
<div>
<input type="checkbox" id="checkbox" >
<label>Regular photoshoot</label>
</div>
<div>
<input type="checkbox" id="checkbox" >
<label>Nature/Outdoors</label>
</div>
<div>
<input type="checkbox" id="checkbox" >
<label>Wedding</label>
</div>
<div>
<input type="checkbox" id="checkbox" >
<label>senior photoshoot</label>
</div>
<div>
<input type="checkbox" id="checkbox" >
<label>Family photoshoot</label>
</div>
<div>
<input type="checkbox" id="checkbox">
<label>Pets/Animals</label>
</div>
<button class="button button1" type="button" onclick="javascript:eraseText();">Submit</button>
</fieldset>
</div>
<script>
function eraseText() {
document.getElementById("output1").value = "";
document.getElementById("output2").value = "";
const checkbox = document.getElementById("checkbox");
checkbox.removeAttribute('checked');
}
</script>
use of jquery as seen on this page https://stackoverflow.com/questions/13557623/remove-attribute-checked-of-checkbox did not change the function and did nothing to the code
答案1
得分: 1
Sure, here is the translated code:
id的值需要唯一,所以将id=checkbox更改为class=checkbox。然后使用document.getElementsByClassName("checkbox")获取所有复选框的数组。最后,将其放入循环中,并设置checkbox[i].checked = false;
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言:lang-js -->
function eraseText() {
document.getElementById("output1").value = "";
document.getElementById("output2").value = "";
const checkbox = document.getElementsByClassName("checkbox");
for (i = 0; i < checkbox.length; i++) {
checkbox[i].checked = false
}
}
<!-- 语言:lang-html -->
<div>
<fieldset>
<legend>预约安排</legend>
<p>您的姓名:
<p>
<textarea name="story" rows="1" cols="50" id='output1'> </textarea>
<p>您的电子邮件:
<p>
<textarea name="story" rows="1" cols="50" id='output2'> </textarea>
<p>日期:</p>
<input type="date" id="start" name="trip-start" value="2023-06-17" min="2018-01-01" max="2035-12-31">
<br>
<div>
<input type="checkbox" class="checkbox">
<label>常规摄影</label>
</div>
<div>
<input type="checkbox" class="checkbox">
<label>大自然/户外</label>
</div>
<div>
<input type="checkbox" class="checkbox">
<label>婚礼</label>
</div>
<div>
<input type="checkbox" class="checkbox">
<label>高级摄影</label>
</div>
<div>
<input type="checkbox" class="checkbox">
<label>家庭摄影</label>
</div>
<div>
<input type="checkbox" class="checkbox">
<label>宠物/动物</label>
</div>
<button class="button button1" type="button" onclick="javascript:eraseText();">提交</button>
</fieldset>
</div>
<!-- 结束代码片段 -->
I have translated the code and removed the parts that were not meant for translation as requested.
英文:
id's need to be unique, so change id=checkbox to class = checkbox. then get the array of all checkboxes with document.getElementsByClassName("checkbox"). Finally put that in a loop and set checkbox[i].checked = false;
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function eraseText() {
document.getElementById("output1").value = "";
document.getElementById("output2").value = "";
const checkbox = document.getElementsByClassName("checkbox");
for (i = 0; i < checkbox.length; i++) {
checkbox[i].checked = false
}
}
<!-- language: lang-html -->
<div>
<fieldset>
<legend>Appointment scheduling</legend>
<p>Your Name:
<p>
<textarea name="story" rows="1" cols="50" id='output1'> </textarea>
<p>Your Email:
<p>
<textarea name="story" rows="1" cols="50" id='output2'> </textarea>
<p>date:</p>
<input type="date" id="start" name="trip-start" value="2023-06-17" min="2018-01-01" max="2035-12-31">
<br>
<div>
<input type="checkbox" class="checkbox">
<label>Regular photoshoot</label>
</div>
<div>
<input type="checkbox" class="checkbox">
<label>Nature/Outdoors</label>
</div>
<div>
<input type="checkbox" class="checkbox">
<label>Wedding</label>
</div>
<div>
<input type="checkbox" class="checkbox">
<label>senior photoshoot</label>
</div>
<div>
<input type="checkbox" class="checkbox">
<label>Family photoshoot</label>
</div>
<div>
<input type="checkbox" class="checkbox">
<label>Pets/Animals</label>
</div>
<button class="button button1" type="button" onclick="javascript:eraseText();">Submit</button>
</fieldset>
</div>
<!-- end snippet -->
答案2
得分: 0
"checked"属性设置复选框的默认状态,而不是当前值。
要修改当前值,请将布尔值分配给"checked" 属性。
checkbox.checked = false;
英文:
The checked
attribute sets the default state of the checkbox, not the current value.
To modify the current value, assign a boolean value to the checked
property.
checkbox.checked = false;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论