当前位置:网站首页>How to determine whether the checkbox in JS is selected
How to determine whether the checkbox in JS is selected
2022-07-07 16:11:00 【Daxiong who loves learning】
In the process of learning, I touch html Content of separate development , Check box checkbox It is determined that the selection is for ajax It is very important to transmit data , So forward this article for retention , For future reference .
To determine whether the check box is selected, you can use :
$("#test1").prop("checked") and $("#test1").is(":checked")1
1、checkbox Medium checked attribute
1.1 If for input label , Do not set its checked Attribute words , The default is that the check box is not selected ;
<input type="checkbox" name="test" id="test"/> // Not selected 1
1.2 And if you set it checked Attribute words , I am js In the middle of the test ;
1) take checked Property is set to checked=true perhaps checked=”true” perhaps checked=”false” when , The system will think that the check box is selected .
<input type="checkbox" name="test" id="test1"/ checked=true> // Choose
<input type="checkbox" name="test" id="test2"/ checked="true"> // Choose
<input type="checkbox" name="test" id="test3"/ checked="false"> // Choose
<input type="checkbox" name="test" id="test4"/ checked > // Choose 1234
2) During the test , take checked Property is set to checked=false when , The system still thinks that the check box is not selected ;
<input type="checkbox" name="test" id="test5"/ checked=false> // Not selected 1
*** however , When we will checked Property is set to checked=false when , Although at the time of display , The check box is not selected when it is rendered , however , If we use the following code :
$("#test").prop("checked") as well as $("#test").is(":checked")1
The value obtained is still true( Choose ).
2、 Use (.val()) Value —– It can't be used to judge whether the check box is selected
<input type="checkbox" name="test" id="test"/> // Not selected
<input type="checkbox" name="test" id="test1"/ checked=true> // Choose 12
We use console.log() take checked Print out the value of , The result is as follows :
Whether it is selected or not , The values taken are on, So we have to decide checkbox Selected state of , Out of commission .val() Value
console.log($("#test").val()) // Print out on
console.log($("#test1").val()) // Print out on12
3、 Use (.attr()) Take the attribute value —– It can't be used to judge whether the check box is selected
<input type="checkbox" name="test" id="test"/> // Not selected
<input type="checkbox" name="test" id="test1"/ checked> // Choose 12
We use console.log() take checked Print out the value of , The result is as follows :
Whether it is selected or not , The values taken are undefine, So we have to decide checkbox Selected state of , Out of commission .attr() Value
1
console.log($("#test").attr()) // Print out undefine
console.log($("#test1").attr()) // Print out undefine12
4、 Use (.prop()) Get current status —— It can be used to judge whether the check box is selected
<input type="checkbox" name="test" id="test"/> // Not selected
<input type="checkbox" name="test" id="test1"/ checked> // Choose 12
We use console.log() take checked Print out the value of , The result is as follows :
The selected status will get true, If it is not selected, you will get false, So we have to decide checkbox Selected state of , have access to .prop() Value
onsole.log($("#test").prop("checked")) // Print out false
console.log($("#test1").prop("checked")) // Print out true12
5、 Use (.is())—- It can be used to judge whether the check box is selected
<input type="checkbox" name="test" id="test"/> // Not selected
<input type="checkbox" name="test" id="test1"/ checked> // Choose 12
Use $(“input[type=’checkbox’]”).is(‘:checked’) sentence , By using console.log() take checked Print out the value of , The result is as follows :
The selected status will get true, If it is not selected, you will get false, So we have to decide checkbox Selected state of , have access to .is() Value
console.log($("#test").is(":checked")) // Print out false
console.log($("#test1").is(":checked")) // Print out true
边栏推荐
- How does geojson data merge the boundaries of regions?
- Vite path alias @ configuration
- Three. JS introductory learning notes 0: illustration of how webgl and threejs work
- 讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
- 尤雨溪,来了!
- SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
- Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
- A wave of open source notebooks is coming
- 应用程序和matlab的通信方式
- 【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
猜你喜欢

C4D learning notes 2- animation - timeline and time function

LeetCode2_ Add two numbers

SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术

numpy---基础学习笔记

A wave of open source notebooks is coming

Syntax of generator function (state machine)

Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)

2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo

讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!

Unity3D_ Class fishing project, bullet rebound effect is achieved
随机推荐
Virtual memory, physical memory /ram what
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
应用程序和matlab的通信方式
Simple understanding and application of TS generics
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
保证接口数据安全的10种方案
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
Shader Language
What else can an ordinary person do besides working in a factory to make money?
Three. JS introductory learning notes 11:three JS group composite object
SPI master RX time out interrupt
Please supervise the 2022 plan
torch. Numel action
hellogolang
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
Vite path alias @ configuration
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
LeetCode1_ Sum of two numbers
Detailed explanation of unity hot update knowledge points and introduction to common solution principles