← Back to Vue.js Course | Chapter 5: Forms | Lesson 3 of 6

Checkboxes/Radios/Selects का उपयोग

Checkboxes, radio buttons, और dropdowns हर एक उस data type से bind होता है जो इनके लिए सबसे ज़्यादा fit करता है।
Syntax
markup
<input type="checkbox" v-model="checked">
<input type="radio" value="option" v-model="picked">
<select v-model="selected">
  <option value="option">text</option>
</select>

Checkboxes/Radios/Selects

एक single checkbox एक boolean से bind होता है, कई checkboxes एक array से bind होते हैं, एक radio group एक value से bind होता है, और एक select chosen option value (या multiple के साथ एक array) से bind होता है। Non-string values bind करने के लिए options और inputs पर :value इस्तेमाल करें।

Note: सारी checked values collect करने के लिए कई checkboxes को एक array से bind करें।

उदाहरण: Checkboxes/radios/selects

markup
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
  <label><input type="checkbox" v-model="agree"> Agree</label>
  <label><input type="checkbox" value="js" v-model="langs"> JS</label>
  <label><input type="checkbox" value="py" v-model="langs"> Python</label>
  <label><input type="radio" value="S" v-model="size"> S</label>
  <label><input type="radio" value="L" v-model="size"> L</label>
  <select v-model="color"><option value="red">Red</option><option value="blue">Blue</option></select>
  <p>{{ agree }} | {{ langs }} | {{ size }} | {{ color }}</p>
</div>
<script>
  Vue.createApp({ data() { return { agree: false, langs: [], size: "S", color: "red" }; } }).mount("#app");
  (async () => {
    const boxes = document.querySelectorAll("input");
    for (const i of [0, 1, 2, 4]) { boxes[i].click(); await Vue.nextTick(); }
    const sel = document.querySelector("select"); sel.value = "blue"; sel.dispatchEvent(new window.Event("change"));
    await Vue.nextTick();
    console.log(document.querySelector("p").textContent);
  })();
</script>
</body>
</html>

<!-- Output:
Rendered: Agree JS Python S LRedBluetrue | [ "js", "py" ] | L | blue
console: true | [
  "js",
  "py"
] | L | blue
-->
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. जहाँ एक array चाहिए वहाँ एक boolean इस्तेमाल करना
  2. value attribute भूल जाना
  3. select के लिए एक initial value set न करना
चैप्टर सारांश
  • एक checkbox एक boolean है
  • कई checkboxes एक array देते हैं
  • Radios एक value देते हैं
  • select chosen value bind करता है
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.