当前位置:网站首页>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
边栏推荐
猜你喜欢
Detailed explanation of BGP message
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
Current situation analysis of Devops and noops
Decryption skills of encrypted compressed files
Spark overview
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
网络相关知识(硬件工程师)
穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
Let every developer use machine learning technology
Deep learning classification network -- vggnet
随机推荐
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
线性dp(拆分篇)
Community theory | kotlin flow's principle and design philosophy
Arduino Wire 库使用
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
Data science [viii]: SVD (I)
Sumo tutorial Hello World
RestTemplate请求时设置请求头,请求参数,请求体。
ROS create workspace
CUDA与Direct3D 一致性
深入学习JVM底层(三):垃圾回收器与内存分配策略
Common means of modeling: combination
Golang -- map capacity expansion mechanism (including source code)
阿里云MFA绑定Chrome浏览器
Network related knowledge (Hardware Engineer)
Top 10 classic MySQL errors
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
Detailed steps of JS foreground parsing of complex JSON data "case: I"
CUDA中的线程层次
State machine in BGP