gpt4 book ai didi

javascript - 没有值的 VueJs 单选按钮会打开所有单选输入元素

转载 作者:行者123 更新时间:2023-12-02 23:46:52 24 4
gpt4 key购买 nike

我不太理解这种行为。

在 View 中,当处理没有设置值属性的单选输入元素时,单击一个单选按钮将打开所有单选按钮。这是为什么?

我知道解决方案是为每个值添加一个值。在常规 HTML 中,这种情况不会发生。

<!DOCTYPE html>
<html>
<head>
<title>My first Vue app</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<style>

</style>
</head>
<body>
<div id="multiple_radios">
<input type="radio" id="PC" v-model="picked">
<label for="PC">PC (Windows)</label>

<input type="radio" id="Mac" v-model="picked">
<label for="Mac">Mac</label>

<p>{{ picked }}</p>
</div>
<script>
const multiple_radios = new Vue({
el: '#multiple_radios',
data : {
picked : ''
}
});
</script>
</body>
</html>

最佳答案

当您不设置该值时,它们都是未定义。因此,当您单击 1 个单选按钮时,picked 值也会设置为 undefined。这就是选择两个单选按钮的原因。

<!DOCTYPE html>
<html>
<head>
<title>My first Vue app</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<style>

</style>
</head>
<body>
<div id="multiple_radios">
<input type="radio" id="PC" v-model="picked">
<label for="PC">PC (Windows)</label>

<input type="radio" id="Mac" v-model="picked">
<label for="Mac">Mac</label>

<p>Is undefined? {{ picked == undefined }}</p>
</div>
<script>
const multiple_radios = new Vue({
el: '#multiple_radios',
data : {
picked : ''
}
});
</script>
</body>
</html>

关于javascript - 没有值的 VueJs 单选按钮会打开所有单选输入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55821085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com