使用复选框在 2 个数字之间切换
Use a checkbox to toggle between 2 numbers
如何使用 JS
/jQuery
中的复选框在 2 个数字之间切换?
我希望未选中的始终为 [=14=]
,选中的始终为 0
。下面的代码很接近。我希望使用 str.replace
而不是使用 display:none
.
切换 divs
代码:
function myFunction() {
let str = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML = str.replace("[=10=]", "0");
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String</h2>
<p>The replace() method searches a string for a specified value, or a regular expression,
and returns a new string where the specified values are replaced.</p>
<p id="demo">[=11=]</p>
<input type="checkbox" onclick="myFunction()" value="Try It">
</body>
</html>
function myFunction() {
const element = document.getElementById('demo')
element.innerText = element.innerText === '[=10=]' ? '0' : '[=10=]'
}
如何使用 JS
/jQuery
中的复选框在 2 个数字之间切换?
我希望未选中的始终为 [=14=]
,选中的始终为 0
。下面的代码很接近。我希望使用 str.replace
而不是使用 display:none
.
divs
代码:
function myFunction() {
let str = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML = str.replace("[=10=]", "0");
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String</h2>
<p>The replace() method searches a string for a specified value, or a regular expression,
and returns a new string where the specified values are replaced.</p>
<p id="demo">[=11=]</p>
<input type="checkbox" onclick="myFunction()" value="Try It">
</body>
</html>
function myFunction() {
const element = document.getElementById('demo')
element.innerText = element.innerText === '[=10=]' ? '0' : '[=10=]'
}