Home > Web Front-end > JS Tutorial > How to determine whether a checkbox is selected using jquery

How to determine whether a checkbox is selected using jquery

PHPz
Release: 2018-09-28 16:59:24
Original
1282 people have browsed it

This article mainly introduces the jquery method to determine whether a check box is selected. Friends in need can refer to it.

Jquery summarizes the problem of determining whether a check box is selected and how to select it.

Let’s get to the point, or when the page has the following set of check boxes:

<input type="checkbox" name="fruit" value="apple">苹果 
<input type="checkbox" name="fruit" value="orange">橘子 
<input type="checkbox" name="fruit" value="banana">香蕉 
<input type="checkbox" name="fruit" value="grape">葡萄
Copy after login

Then use Jquery to get a set of name=fruit The method of checking the value of the check box is as follows:

var checkVal=&#39;&#39;; 
$("input[name=&#39;fruit&#39;]:checkbox").each(function(){ 
  if($(this).attr(&#39;checked&#39;)){ 
    checkVal+=$(this).val()+&#39;,&#39;; 
  } 
}) 
判断 name=fruit 组的复选框是否有被选中的选项:
var flag=false; 
$("input[name=&#39;fruit&#39;]:checkbox").each(function(){ 
  if($(this).attr(&#39;checked&#39;)){ 
    flag=true; 
  } 
}) 
if(flag){ 
  alert(&#39;有被选中&#39;); 
}else{ 
  alert(&#39;没有选中任何选项&#39;); 
}
Copy after login

The above is the method of jquery to determine whether the check box is selected. I hope it will be helpful to everyone's study. That’s all the content of this chapter. For more related tutorials, please visit jQuery Video Tutorial!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template