尝试检查页面上是否有任何复选框被选中。

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

Trying to check if any checkbox on the page is checked

问题

我想检查当在名称文本框中输入文本时,网站上的任何复选框是否已被选中。我知道事件监听器适用于名称,因为这可以在没有 document.getElementsByName("event[]").checked 的情况下工作,但如何使其适用于复选框?

document.getElementsByName("name")[0].addEventListener('change', (event) => {
  if (event.target.value.length != 0 && document.getElementsByName("event[]").checked ) {
    window.alert("checked");
  }
})
<form id=&quot;bookingForm&quot; action=&quot;javascript:alert(&#39;form submitted&#39;);&quot; method=&quot;get&quot;&gt;
  <section id=&quot;Events&quot;&gt;
    <h2&gt;Select Events&lt;/h2&gt;
    <div class=&#39;item&#39;&gt;
      <span class=&#39;eventTitle&#39;&gt;Event number 1&lt;/span&gt;
      <span class=&#39;eventPrice&#39;&gt;10.50&lt;/span&gt;
      <span class=&#39;chosen&#39;&gt;&lt;input type=&#39;checkbox&#39; name=&#39;event[]&#39; value=&#39;1&#39; data-price=&#39;10.50&#39;&gt;&lt;/span&gt;
    </div&gt;
    <div class=&#39;item&#39;&gt;
      <span class=&#39;eventTitle&#39;&gt;Event number 2&lt;/span&gt;
      <span class=&#39;eventPrice&#39;&gt;5.00&lt;/span&gt;
      <span class=&#39;chosen&#39;&gt;&lt;input type=&#39;checkbox&#39; name=&#39;event[]&#39; value=&#39;2&#39; data-price=&#39;5.00&#39;&gt;&lt;/span&gt;
    </div&gt;
    <section id=&quot;Cost&quot;&gt;
      &lt;input type=&quot;radio&quot; name=&quot;delivery&quot; value=&quot;ticket&quot; data-price=&quot;10&quot;&gt;
      &lt;h2&gt;Total Price&lt;/h2&gt;
      Total Price &lt;input type=&quot;text&quot; name=&quot;total&quot; size=&quot;12&quot;&gt;
      &lt;p&gt;Name&lt;input type=&quot;text&quot; name=&quot;name&quot;&gt;&lt;/p&gt;
    </section&gt;

    &lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Book&quot;&gt;&lt;/p&gt;
  </section&gt;
</form&gt;
英文:

I want to check if any checkbox on the website has been checked when text has been entered in the name text box. I know that the EventListener works for name since this works without the document.getElementsByName("event[]").checked, but how do I make it work for the checkboxes?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
document.getElementsByName("name")[0].addEventListener('change', (event) => {
if (event.target.value.length != 0 && document.getElementsByName("event[]").checked ) {
window.alert("checked");
}
})

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

&lt;form id=&quot;bookingForm&quot; action=&quot;javascript:alert(&#39;form submitted&#39;);&quot; method=&quot;get&quot;&gt;
&lt;section id=&quot;Events&quot;&gt;
&lt;h2&gt;Select Events&lt;/h2&gt;
&lt;div class=&#39;item&#39;&gt;
&lt;span class=&#39;eventTitle&#39;&gt;Event number 1&lt;/span&gt;
&lt;span class=&#39;eventPrice&#39;&gt;10.50&lt;/span&gt;
&lt;span class=&#39;chosen&#39;&gt;&lt;input type=&#39;checkbox&#39; name=&#39;event[]&#39; value=&#39;1&#39; data-price=&#39;10.50&#39;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&#39;item&#39;&gt;
&lt;span class=&#39;eventTitle&#39;&gt;Event number 2&lt;/span&gt;
&lt;span class=&#39;eventPrice&#39;&gt;5.00&lt;/span&gt;
&lt;span class=&#39;chosen&#39;&gt;&lt;input type=&#39;checkbox&#39; name=&#39;event[]&#39; value=&#39;2&#39; data-price=&#39;5.00&#39;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;section id=&quot;Cost&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;delivery&quot; value=&quot;ticket&quot; data-price=&quot;10&quot;&gt;
&lt;h2&gt;Total Price&lt;/h2&gt;
Total Price &lt;input type=&quot;text&quot; name=&quot;total&quot; size=&quot;12&quot;&gt;
&lt;p&gt;Name&lt;input type=&quot;text&quot; name=&quot;name&quot;&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Book&quot;&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;/form&gt;

<!-- end snippet -->

答案1

得分: 1

Here is the translated code portion:

我建议

document.querySelector("input[name=name]").addEventListener('change', (event) => {
  if (event.target.value.trim().length > 0 &&
      document.querySelector('input[type=checkbox]:checked') !== null) {
    window.alert("checked");
  }
});

Please note that I've translated the code only, as per your request. If you have any more code to translate, feel free to share it.

英文:

I'd suggest:

document.getElementsByName(&quot;name&quot;)[0].addEventListener(&#39;change&#39;, (event) =&gt; {
// here we used event.target.value.trim.length in order to guard against
// white-space strings being considered valid (if that&#39;s not a problem
// then the trim() method can be removed), and also we used:
// document.querySelector() to find the first of any &lt;input&gt; element
// with a type equal to &#39;checkbox&#39; which is also checked;
// document.querySelector() returns either the first such element or null:
if (event.target.value.trim().length &gt; 0 &amp;&amp; 
document.querySelector(&#39;input[type=checkbox]:checked&#39;) !== null) {
window.alert(&quot;checked&quot;);
}
});

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-js -->

document.getElementsByName(&quot;name&quot;)[0].addEventListener(&#39;change&#39;, (event) =&gt; {
if (event.target.value.trim().length &gt; 0 &amp;&amp;
document.querySelector(&#39;input[type=checkbox]:checked&#39;) !== null) {
window.alert(&quot;checked&quot;);
}
})

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

&lt;form id=&quot;bookingForm&quot; action=&quot;javascript:alert(&#39;form submitted&#39;);&quot; method=&quot;get&quot;&gt;
&lt;section id=&quot;Events&quot;&gt;
&lt;h2&gt;Select Events&lt;/h2&gt;
&lt;div class=&#39;item&#39;&gt;
&lt;span class=&#39;eventTitle&#39;&gt;Event number 1&lt;/span&gt;
&lt;span class=&#39;eventPrice&#39;&gt;10.50&lt;/span&gt;
&lt;span class=&#39;chosen&#39;&gt;&lt;input type=&#39;checkbox&#39; name=&#39;event[]&#39; value=&#39;1&#39; data-price=&#39;10.50&#39;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&#39;item&#39;&gt;
&lt;span class=&#39;eventTitle&#39;&gt;Event number 2&lt;/span&gt;
&lt;span class=&#39;eventPrice&#39;&gt;5.00&lt;/span&gt;
&lt;span class=&#39;chosen&#39;&gt;&lt;input type=&#39;checkbox&#39; name=&#39;event[]&#39; value=&#39;2&#39; data-price=&#39;5.00&#39;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;section id=&quot;Cost&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;delivery&quot; value=&quot;ticket&quot; data-price=&quot;10&quot;&gt;
&lt;h2&gt;Total Price&lt;/h2&gt;
Total Price &lt;input type=&quot;text&quot; name=&quot;total&quot; size=&quot;12&quot;&gt;
&lt;p&gt;Name&lt;input type=&quot;text&quot; name=&quot;name&quot;&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Book&quot;&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;/form&gt;

<!-- end snippet -->

It's worth adding that your first line could be potentially simplified a little using document.querySelector() rather than using indices:

document.querySelector(&quot;input[name=name]&quot;).addEventListener(&#39;change&#39;, (event) =&gt; {
if (event.target.value.trim().length &gt; 0 &amp;&amp; 
document.querySelector(&#39;input[type=checkbox]:checked&#39;) !== null) {
window.alert(&quot;checked&quot;);
}
});

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-js -->

document.querySelector(&#39;input[name=name]&#39;).addEventListener(&#39;change&#39;, (event) =&gt; {
if (event.target.value.trim().length &gt; 0 &amp;&amp;
document.querySelector(&#39;input[type=checkbox]:checked&#39;) !== null) {
window.alert(&quot;checked&quot;);
}
});

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

&lt;form id=&quot;bookingForm&quot; action=&quot;javascript:alert(&#39;form submitted&#39;);&quot; method=&quot;get&quot;&gt;
&lt;section id=&quot;Events&quot;&gt;
&lt;h2&gt;Select Events&lt;/h2&gt;
&lt;div class=&#39;item&#39;&gt;
&lt;span class=&#39;eventTitle&#39;&gt;Event number 1&lt;/span&gt;
&lt;span class=&#39;eventPrice&#39;&gt;10.50&lt;/span&gt;
&lt;span class=&#39;chosen&#39;&gt;&lt;input type=&#39;checkbox&#39; name=&#39;event[]&#39; value=&#39;1&#39; data-price=&#39;10.50&#39;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&#39;item&#39;&gt;
&lt;span class=&#39;eventTitle&#39;&gt;Event number 2&lt;/span&gt;
&lt;span class=&#39;eventPrice&#39;&gt;5.00&lt;/span&gt;
&lt;span class=&#39;chosen&#39;&gt;&lt;input type=&#39;checkbox&#39; name=&#39;event[]&#39; value=&#39;2&#39; data-price=&#39;5.00&#39;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;section id=&quot;Cost&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;delivery&quot; value=&quot;ticket&quot; data-price=&quot;10&quot;&gt;
&lt;h2&gt;Total Price&lt;/h2&gt;
Total Price &lt;input type=&quot;text&quot; name=&quot;total&quot; size=&quot;12&quot;&gt;
&lt;p&gt;Name&lt;input type=&quot;text&quot; name=&quot;name&quot;&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Book&quot;&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;/form&gt;

<!-- end snippet -->

References:

答案2

得分: 0

//--获取所有复选框,您可以另一种方式来获取复选框
var checkBoxes = document.getElementsByTagName("input");
//--对于每个复选框,检查是否被选中
for (var i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i].checked == true) {
alert('已选中');
}
}

英文:
//--Get all checkboxes, you can have another way of fetching checkboxes
var checkBoxes = document.getElementsByTagName(&quot;input&quot;);
//--For each checkbox, check if it is selected
for (var i = 0; i &lt; checkBoxes.length; i++) {
if (checkBoxes[i].checked == true) {
alert(&#39;checked&#39;);
}
}

答案3

得分: 0

Use document.querySelectorAll 方法来获取包含文档中所有复选框输入的数组:

var checkboxes = document.querySelectorAll("input[type=checkbox]");

然后,要检查是否有任何一个复选框被选中:

var anyChecked = false;
for (var i = 0; i < checkboxes.length; i++) {
  if (checkboxes[i].checked) {
    anyChecked = true;
    break;
  }
}

或者,如果你想要一个只包含已选中复选框的数组,你可以这样做:

var checkedCheckboxes = Array.from(checkboxes).filter(checkbox => checkbox.checked);
英文:

Use document.querySelectorAll to get an array containing all the checkbox inputs in the document:

var checkboxes = document.querySelectorAll(&quot;input[type=checkbox]&quot;);

Then, to check if any of the checkboxes are checked:

var anyChecked = false;
for (var i = 0; i &lt; checkboxes.length; i++) {
  if (checkboxes[i].checked) {
    anyChecked = true;
    break;
  }
}

Alternatively, if you want an array containing only the checkboxes that are checked, you can do the following:

var checkedCheckboxes = checkboxes.filter(checkbox =&gt; checkbox.checked);

答案4

得分: 0

document.getElementsByName("name")[0].addEventListener('change', (event) => {
if (event.target.value.length !== 0) {
// 获取所有复选框
const checkboxes = document.getElementsByName("event[]");
for (i = 0; i < checkboxes.length; i++) {
// 检查复选框是否被选中
if (checkboxes[i].checked) {
window.alert("已选中");
}
}
}
})

英文:
document.getElementsByName(&quot;name&quot;)[0].addEventListener(&#39;change&#39;, (event) =&gt; {
if(event.target.value.length !== 0) {
// Get all checkboxes  
const checkboxes = document.getElementsByName(&quot;event[]&quot;);
for(i=0; i&lt;checkboxes.length; i++) {
// Check if checkbox is checked or not
if(checkboxes[i].checked) {
window.alert(&quot;checked&quot;);
}
}
}
})

huangapple
  • 本文由 发表于 2020年1月6日 22:58:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614294.html
匿名

发表评论

匿名网友

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

确定