当前位置:网站首页>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
边栏推荐
- Vs tool word highlight with margin
- Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
- Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
- asyncio 概念和用法
- 星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
- Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
- 10 schemes to ensure interface data security
- 统计学习方法——感知机
- 招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
- 95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
猜你喜欢

Async and await

TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比

Numpy --- basic learning notes

Three. JS introductory learning notes 10:three JS grid

TS as a general cache method

Ue4/ue5 multi thread development attachment plug-in download address
通知Notification使用全解析
![[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3](/img/8f/ca9ab042916f68de7994d9f2124da9.jpg)
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3

Wireless sensor networks -- ZigBee and 6LoWPAN

尤雨溪,来了!
随机推荐
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
强化实时数据管理,英方软件助力医保平台安全建设
C4D learning notes 2- animation - timeline and time function
Function: JS Click to copy content function
Three. JS introductory learning notes 03: perspective projection camera
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
Three. JS introductory learning notes 13: animation learning
分步式监控平台zabbix
torch.numel作用
Limit of total fields [1000] in index has been exceeded
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
融云斩获 2022 中国信创数字化办公门户卓越产品奖!
121. The best time to buy and sell stocks
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
Leetcode-231-2的幂
Use of SVN
招标公告:盘锦市人民医院盘锦医院数据库维保项目
Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
Step by step monitoring platform ZABBIX