表单绑定的基本用法

下面的代码实现的有:单行文本、多行文本、复选框(checkbox)、单选按钮(radio)、选择框(selected)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="app">
    <input v-model="message" placeholder="edit me">
    <p>Message : {{message}}</p>
    
    <textarea v-model="message2"placeholder="multiple lines">
    </textarea>
    <p>Message2 : {{message2}}</p>
    
    <!-- 多个复选框,绑定到同一个数组 -->
     <input type="checkbox" id="jack" value="Jack" v-model="checkName">
      <label for="jack">Jack</label>
      <input type="checkbox" id="john" value="John" v-model="checkName">
      <label for="john">John</label>
      <input type="checkbox" id="mike" value="Mike" v-model="checkName">
      <label for="mike">Mike</label>
    <p>复选按钮 : {{checkName}}</p>
    
      <input type="radio" id="one" value="One" v-model="picked">
      <label for="one">One</label>
      <input type="radio" id="two" value="Two" v-model="picked">
      <label for="two">Two</label>
     <input type="radio" id="three" value="Three" v-model="picked">
     <label for="three">three</label> 
      <p>单选按钮 : {{ picked }}</p>
      
    <!-- 单个复选框,绑定到布尔值 -->
    <input type="checkbox" id="checkbox" v-model="checked">
    <label for="checkbox"> {{ checked }}</label>
    <br>
    <br>
    
    <select v-model="selected">
       <option value="">请选择</option>
       <option>A</option>
       <option>B</option>
       <option>C</option>
     </select>
     <span>Selected: {{ selected }}</span>
     <br>
     <br>
    
     <select v-model="selecteds" multiple style="width: 50px;">
         <option>A</option>
         <option>B</option>
         <option>C</option>
       </select>
       <br>
       <span>Selecteds: {{ selecteds }}</span>
</div>
<script type="text/javascript">
var example2 = new Vue({
  el: ‘#app‘,
  data:{
      message:"hello",
      message2:"duohang",
      checkName:[‘Jack‘],
      picked:‘‘,
      checked:false,//这里是布尔值,即使初始化不是布尔值也会转换为布尔值
      selected:‘‘,
      selecteds:[‘A‘]
  },
  })
</script>
<style type="text/css">

</style>
</body>
</html>

多选时的选择框是不是有问题?还是只能选择一个。

以上代码大部分来自官方文档https://cn.vuejs.org/v2/guide/forms.html#%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95

以及一个博客:https://blog.csdn.net/qq_35713752/article/details/78542787

相关推荐