当前位置:网站首页>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
边栏推荐
- 稀疏数组(非线性结构)
- 深入学习JVM底层(二):HotSpot虚拟机对象
- 【张三学C语言之】—深入理解数据存储
- Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
- Google Play Academy 组队 PK 赛,正式开赛!
- 深入学习JVM底层(四):类文件结构
- RestTemplate请求时设置请求头,请求参数,请求体。
- WLAN相关知识点总结
- Let every developer use machine learning technology
- Codeforces Round #797 (Div. 3) A—E
猜你喜欢

Zhuanzhuanben - LAN construction - Notes

Detailed explanation of BGP message

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

来自读者们的 I/O 观后感|有奖征集获奖名单

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

Support new and old imperial CMS collection and warehousing tutorials

Sentinel 阿里开源流量防护组件

I/o impressions from readers | prize collection winners list

Deep learning classification network -- alexnet

Sentinel规则持久化到Nacos
随机推荐
495. Timo attack
注解和反射详解以及运用
Sumo tutorial Hello World
深入了解JUC并发(二)并发理论
The Chinese word segmentation task is realized by using traditional methods (n-gram, HMM, etc.), neural network methods (CNN, LSTM, etc.) and pre training methods (Bert, etc.)
深入学习JVM底层(三):垃圾回收器与内存分配策略
Flutter 混合开发: 开发一个简单的快速启动框架 | 开发者说·DTalk
LeetCode 77. combination
Talking about MySQL database
Singleton mode compilation
利用NVIDIA GPU将Minecraft场景渲染成真实场景
Golang--map扩容机制(含源码)
Amazon AWS data Lake Work Pit 1
BGP报文详细解释
I/o multiplexing & event driven yyds dry inventory
TensorRT的功能
最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
LeetCode 47. Full arrangement II
Summary of WLAN related knowledge points
LeetCode 90. Subset II