当使用<input>复选框时,我无法使用JavaScript取消选中复选框。

huangapple go评论75阅读模式
英文:

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?

&lt;div&gt;
    &lt;fieldset&gt;
    &lt;legend&gt;Appointment scheduling&lt;/legend&gt;
    
    &lt;p&gt;Your Name: &lt;p&gt;
    &lt;textarea name=&quot;story&quot; rows=&quot;1&quot; cols=&quot;50&quot; id=&#39;output1&#39;&gt; &lt;/textarea&gt;
    &lt;p&gt;Your Email: &lt;p&gt;
    &lt;textarea name=&quot;story&quot; rows=&quot;1&quot; cols=&quot;50&quot; id=&#39;output2&#39;&gt; &lt;/textarea&gt;
    &lt;p&gt;date:&lt;/p&gt;
    &lt;input type=&quot;date&quot; id=&quot;start&quot; name=&quot;trip-start&quot; value=&quot;2023-06-17&quot; min=&quot;2018-01-01&quot; max=&quot;2035-12-31&quot;&gt;
    &lt;br&gt;

    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; &gt;
      &lt;label&gt;Regular photoshoot&lt;/label&gt;
    &lt;/div&gt;

    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; &gt;
      &lt;label&gt;Nature/Outdoors&lt;/label&gt;
    &lt;/div&gt;
    
    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; &gt;
      &lt;label&gt;Wedding&lt;/label&gt;
    &lt;/div&gt;
    
    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; &gt;
      &lt;label&gt;senior photoshoot&lt;/label&gt;
    &lt;/div&gt;
    
    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; &gt;
      &lt;label&gt;Family photoshoot&lt;/label&gt;
    &lt;/div&gt;
    
    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot;&gt;
      &lt;label&gt;Pets/Animals&lt;/label&gt;
    &lt;/div&gt;
    
    &lt;button class=&quot;button button1&quot; type=&quot;button&quot; onclick=&quot;javascript:eraseText();&quot;&gt;Submit&lt;/button&gt;
&lt;/fieldset&gt;
&lt;/div&gt;

 &lt;script&gt;
        function eraseText() {
          document.getElementById(&quot;output1&quot;).value = &quot;&quot;;
          document.getElementById(&quot;output2&quot;).value = &quot;&quot;;

          const checkbox = document.getElementById(&quot;checkbox&quot;);
          checkbox.removeAttribute(&#39;checked&#39;);
        }
  &lt;/script&gt;

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?

&lt;div&gt;
    &lt;fieldset&gt;
    &lt;legend&gt;Appointment scheduling&lt;/legend&gt;
    
    &lt;p&gt;Your Name: &lt;p&gt;
    &lt;textarea name=&quot;story&quot; rows=&quot;1&quot; cols=&quot;50&quot; id=&#39;output1&#39;&gt; &lt;/textarea&gt;
    &lt;p&gt;Your Email: &lt;p&gt;
    &lt;textarea name=&quot;story&quot; rows=&quot;1&quot; cols=&quot;50&quot; id=&#39;output2&#39;&gt; &lt;/textarea&gt;
    &lt;p&gt;date:&lt;/p&gt;
    &lt;input type=&quot;date&quot; id=&quot;start&quot; name=&quot;trip-start&quot; value=&quot;2023-06-17&quot; min=&quot;2018-01-01&quot; max=&quot;2035-12-31&quot;&gt;
    &lt;br&gt;

    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; &gt;
      &lt;label&gt;Regular photoshoot&lt;/label&gt;
    &lt;/div&gt;

    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; &gt;
      &lt;label&gt;Nature/Outdoors&lt;/label&gt;
    &lt;/div&gt;
    
    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; &gt;
      &lt;label&gt;Wedding&lt;/label&gt;
    &lt;/div&gt;
    
    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; &gt;
      &lt;label&gt;senior photoshoot&lt;/label&gt;
    &lt;/div&gt;
    
    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; &gt;
      &lt;label&gt;Family photoshoot&lt;/label&gt;
    &lt;/div&gt;
    
    &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot;&gt;
      &lt;label&gt;Pets/Animals&lt;/label&gt;
    &lt;/div&gt;
    
    &lt;button class=&quot;button button1&quot; type=&quot;button&quot; onclick=&quot;javascript:eraseText();&quot;&gt;Submit&lt;/button&gt;
&lt;/fieldset&gt;
&lt;/div&gt;

 &lt;script&gt;
        function eraseText() {
          document.getElementById(&quot;output1&quot;).value = &quot;&quot;;
          document.getElementById(&quot;output2&quot;).value = &quot;&quot;;

          const checkbox = document.getElementById(&quot;checkbox&quot;);
          checkbox.removeAttribute(&#39;checked&#39;);
        }
  &lt;/script&gt;

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 Babelfalse -->

<!-- 语言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(&quot;output1&quot;).value = &quot;&quot;;
document.getElementById(&quot;output2&quot;).value = &quot;&quot;;
const checkbox = document.getElementsByClassName(&quot;checkbox&quot;);
for (i = 0; i &lt; checkbox.length; i++) {
checkbox[i].checked = false
}
}

<!-- language: lang-html -->

&lt;div&gt;
&lt;fieldset&gt;
&lt;legend&gt;Appointment scheduling&lt;/legend&gt;
&lt;p&gt;Your Name:
&lt;p&gt;
&lt;textarea name=&quot;story&quot; rows=&quot;1&quot; cols=&quot;50&quot; id=&#39;output1&#39;&gt; &lt;/textarea&gt;
&lt;p&gt;Your Email:
&lt;p&gt;
&lt;textarea name=&quot;story&quot; rows=&quot;1&quot; cols=&quot;50&quot; id=&#39;output2&#39;&gt; &lt;/textarea&gt;
&lt;p&gt;date:&lt;/p&gt;
&lt;input type=&quot;date&quot; id=&quot;start&quot; name=&quot;trip-start&quot; value=&quot;2023-06-17&quot; min=&quot;2018-01-01&quot; max=&quot;2035-12-31&quot;&gt;
&lt;br&gt;
&lt;div&gt;
&lt;input type=&quot;checkbox&quot; class=&quot;checkbox&quot;&gt;
&lt;label&gt;Regular photoshoot&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;checkbox&quot; class=&quot;checkbox&quot;&gt;
&lt;label&gt;Nature/Outdoors&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;checkbox&quot; class=&quot;checkbox&quot;&gt;
&lt;label&gt;Wedding&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;checkbox&quot; class=&quot;checkbox&quot;&gt;
&lt;label&gt;senior photoshoot&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;checkbox&quot; class=&quot;checkbox&quot;&gt;
&lt;label&gt;Family photoshoot&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;checkbox&quot; class=&quot;checkbox&quot;&gt;
&lt;label&gt;Pets/Animals&lt;/label&gt;
&lt;/div&gt;
&lt;button class=&quot;button button1&quot; type=&quot;button&quot; onclick=&quot;javascript:eraseText();&quot;&gt;Submit&lt;/button&gt;
&lt;/fieldset&gt;
&lt;/div&gt;

<!-- 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;

huangapple
  • 本文由 发表于 2023年5月18日 02:52:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定