Javascript implements check box selection attribute_javascript skills
Anyone who is familiar with web front-end development knows that judging whether a check box is selected is something that is often done. There are many ways to judge, but the compatibility of these methods is often ignored during the development process, and the effect is achieved. I used to use many methods as a blogger. I often found articles on Google that were bad about this or that, and I was confused later on. I happened to see a foreign blog today and thought it explained it very well. I plan to translate it into Chinese and add some of my own opinions.
If you are engaged in web development and there are check boxes in the web pages you develop, you may need to determine whether the check box is currently selected and then execute some conditional statements. There are many ways to determine whether a checkbox is checked.
Let us first take a look at how native javascript determines this attribute. In JavaScript, after you select an element, you can easily determine whether the checkbox you selected is checked through the checked attribute of the element.
Let’s look at an example. There is a checkbox on your page and the checkbox has a unique id, such as myCheckBox, as shown below:
Now we first select this element through javascript and then get its checked attribute.
Function checkCheckBox() {
If (document.getElementById('myCheckBox').checked) {
//change it to alert('Its Checked'); if you not working with console
console.log('Its Checked');
} else {
console.log('No its not Checked');
}
}
As you can see, we first select the element by id and then determine its checked attribute. If the check box is selected, its value is true. If the check box is not selected, its value will be false.
If you are using jQuery and you don’t want to use native javascript to make this judgment, there are many ways:
Use is(':checked')
In this usage you will use jQuery’s is() function. The function of this function is to determine whether the selected element or element collection meets the condition parameters you pass to the function. If the conditions are met, it returns true, otherwise it returns false.
So in order to use this function, we need to select the element and then check the value of the selector:checked. This selector works for checkboxes, radio buttons and select tags.
[/code]
$('input[type="button"]').click(function () {
If ($('#myCheckBox').is(':checked')) {
//change it to alert('Its Checked'); if you not working with console
console.log('Its Checked');
} else {
console.log('No its not Checked');
}
});
[/code]
Use prop()
Before jQuery 1.6, the function attr() was used to obtain the properties and attributes of elements, but it was very confusing. So after jQuery 1.6, a new function prop() is used to obtain the current property value of the element.
But before that, we first need to understand the difference between property and attributes. Attributes are some attribute values that you set for HTML tags. This includes the class and id you set for a tag and even the initial value you set for the input box. If you do not set the attribute value in the tag but get the attribute value through attr(), undefined will occur. prop() is also used to obtain the attribute value of an element, but the obvious difference from attr() is that even if the attribute you want to obtain is not defined in the html tag, it can still correctly return the required current attribute value.
According to official recommendations: properties with two attributes of true and false, such as checked, selected or disabled, use prop(), and others use attr().
In order to intuitively reflect the difference between the two, you can change the value of the input box immediately after the page is loaded. At this time, you will find that even if the value of your input box changes, use attr() to obtain it. The property value will not change as the text changes, but the property value obtained through property() will change as the content of the text box changes.
Look at an example, here we have an input box with an initial value set and some attribute sets:
Then in the JQuery code we write:
console.log('Attribute Value is : ' $('#myTextBox').attr('value'));
console.log('Property Value is : ' $('#myTextBox').prop('value'));
We will find that the value in the input box obtained through prop() is always synchronized with the value in it, while the value in the input box obtained through attr() is always set in the html tag. value.
$('input[type="button"]').click(function () {
alert('Attribute Value is : ' $('#myTextBox').attr('value'));
alert('Property Value is : ' $('#myTextBox').prop('value'));
});
Use filter :checked
var isChecked = $('#myCheckBox:checked').length > 0;
Another method for judging the value of this attribute is to add a filter: checked when selecting elements, and then judge the attribute of the element based on the length of the obtained element. But this usage is not recommended, because when you have many groups of checkboxes on your page and use the class selector instead of the id selector, the answers you get may be wrong.
$('input[type="button"]').click(function () {
If ($('#myCheckBox:checked').length > 0 ) {
//change it to alert('Its Checked'); if you not working with console
console.log('Its Checked');
} else {
console.log('No its not Checked');
}
});
We can see that we have several methods to obtain the selected attribute of the check item. This is exactly what web developers often need to use and get confused when choosing the correct way to use it.
The above is the entire content of this article. I hope it will be helpful to everyone learning javascript.
Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function