当前位置:网站首页>Does the assignment of Boolean types such as tag attribute disabled selected checked not take effect?
Does the assignment of Boolean types such as tag attribute disabled selected checked not take effect?
2022-07-02 06:22:00 【Can't be lacking, can't be excessive】
Native html
Set up false the truth is that true
<select>
<option>1</option>
<option>2</option>
<option selected>3</option>
<option selected=false>4</option>
</select>
The above code is actually select Selected by default 4
In fact, no matter you let selected=0 selected=false selected=none wait , It is considered to be set to true
Because in the original html in , The assignment of all attributes is actually a string , Assign a string to a Boolean attribute , Will be forcibly converted to Boolean , therefore "false" =>true “0” => true …
So in a sense , We can only use js perhaps jquery Give it a Boolean value
In this way, the default selection is no 4 individual
<select>
<option>1</option>
<option>2</option>
<option selected>3</option>
<option id="option4">4</option>
</select>
var option4 = document.getElementById("option4")
option4.selected = true
react The Boolean property in can only be set to Boolean
Set up false Considered invalid , Default choice 3
<select>
<option>1</option>
<option>2</option>
<option selected>3</option>
<option selected={
false}>4</option>
</select>
Set up undefined It's actually false, The final default choice 3
<select>
<option>1</option>
<option>2</option>
<option selected>3</option>
<option selected={
undefined}>4</option>
</select>
Of course, you can also set an expression to judge , Default choice 4
import React, {
useState } from "react";
import {
Link } from "react-router-dom";
const Login: React.FC = () => {
const [options, setOptions] = useState([1, 2, 3, 4])
return (
<div>
<Link to="/home">Home</Link>
<select>
{
options.map((item) => {
return <option key={
item} selected={
item === 4} >
{
item}
</option>
})
}
</select>
</div>
);
}
export default Login;
But this may give a warning react-dom.development.js:86 Warning: Use the defaultValue or value props on instead of setting selected on .
It means to let you directly in select Label settings value perhaps defaultValue Rather than to option Set up selected
In this way, the default selection is 2
import React, {
useState } from "react";
import {
Link } from "react-router-dom";
const Login: React.FC = () => {
const [options, setOptions] = useState([1, 2, 3, 4])
return (
<div>
<Link to="/home">Home</Link>
<select value={
2}>
{
options.map((item) => {
return <option key={
item} >
{
item}
</option>
})
}
</select>
</div>
);
}
export default Login;
react Whether other Boolean attributes in are related to selected In the same way
Only the first one 4 individual input Disabled
import React, {
useState } from "react";
import {
Link } from "react-router-dom";
const Login: React.FC = () => {
const [options, setOptions] = useState([1, 2, 3, 4])
return (
<div>
<Link to="/home">Home</Link>
{
options.map((item) => {
return <input key={
item} disabled={
item === 4} />
})
}
</div>
);
}
export default Login;
Only the first one 4 individual checkbox To be selected
import React, {
useState } from "react";
import {
Link } from "react-router-dom";
const Login: React.FC = () => {
const [options, setOptions] = useState([1, 2, 3, 4])
return (
<div>
<Link to="/home">Home</Link>
{
options.map((item) => {
return <input type="checkbox" key={
item} checked={
item === 4} />
})
}
</div>
);
}
export default Login;
be-all input Are not read Pattern , It can be input normally
import React, {
useState } from "react";
import {
Link } from "react-router-dom";
const Login: React.FC = () => {
const [options, setOptions] = useState([1, 2, 3, 4])
return (
<div>
<Link to="/home">Home</Link>
{
options.map((item) => {
return <input key={
item} readOnly={
false} />
})
}
</div>
);
}
export default Login;
After a variety of Boolean tests ,react The Boolean attribute performance in is consistent
vue Set Boolean property in
Set to... Separately false, It's actually true. In fact, the default selection is 3
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4" :selected="false">4</option>
</select>
If you pass data The way of attributes , Can achieve false The value of the , The default is 3
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4" :selected="selected1">4</option>
</select>
export default class Home extends Vue {
selected1 = false
}
</script>
Of course, the judgement is also ok , General choice === How to judge , By default, the second option is selected 2 individual
<select>
<option v-for="item in options" :key="item" value="item" :selected="item === 2">
{
{
item }}
</option>
</select>
export default class Home extends Vue {
options = [1, 2, 3, 4]
}
</script>
vue Other Boolean attributes are similar to selected Is it consistent ?
Only the second input Disabled
<input v-for="item in options" :key="item" value="item" :disabled="item === 2" />
export default class Home extends Vue {
options = [1, 2, 3, 4]
}
All false take effect Are not disabled
<input v-for="item in options" :key="item" value="item" :disabled="false" />
All true take effect Are disabled
<input v-for="item in options" :key="item" value="item" :disabled="true" />
checked disabled readonly Basically, all Boolean types behave like this
边栏推荐
- Decryption skills of encrypted compressed files
- Let every developer use machine learning technology
- Style modification of Mui bottom navigation
- Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
- Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table
- Ros2 --- lifecycle node summary
- 链表(线性结构)
- BGP 路由优选规则和通告原则
- Redis---1. Data structure characteristics and operation
- LeetCode 27. 移除元素
猜你喜欢

深入学习JVM底层(二):HotSpot虚拟机对象

Support new and old imperial CMS collection and warehousing tutorials

Sumo tutorial Hello World

Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table

找到页面当前元素z-index最高的数值

Linear DP (split)

Web components series (VIII) -- custom component style settings

CUDA中的线程层次

穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!

日期时间API详解
随机推荐
LeetCode 83. 删除排序链表中的重复元素
No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
浅谈三点建议为所有已经毕业和终将毕业的同学
Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
Community theory | kotlin flow's principle and design philosophy
The real definition of open source software
Golang -- map capacity expansion mechanism (including source code)
LeetCode 83. Delete duplicate elements in the sorting linked list
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
Pbootcms collection and warehousing tutorial quick collection release
介绍两款代码自动生成器,帮助提升工作效率
TensorRT的命令行程序
Data science [9]: SVD (2)
LeetCode 40. Combined sum II
ROS create workspace
LeetCode 283. Move zero
In depth understanding of JUC concurrency (II) concurrency theory
【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
Let every developer use machine learning technology
Bgp Routing preference Rules and notice Principles