Javascript Vue单选按钮v-if和v-model

Javascript Vue单选按钮v-if和v-model,javascript,html,json,vue.js,Javascript,Html,Json,Vue.js,我对html和vue非常陌生,并尝试创建单选按钮。我使用用于创建UI的本地JSON文件。我循环遍历JSON对象并相应地创建单选按钮。我使用“defaultedOptionId”查找要作为默认值检查的选项。这是我的JSON: { "id": 1, "selected": 1, "defaultedOptionId": 2, "selections": [ { "selectionId": 1, "description: "XX" },

我对html和vue非常陌生,并尝试创建单选按钮。我使用用于创建UI的本地JSON文件。我循环遍历JSON对象并相应地创建单选按钮。我使用“defaultedOptionId”查找要作为默认值检查的选项。这是我的JSON:

{ "id": 1,
  "selected": 1,
  "defaultedOptionId": 2,
  "selections": [
    {
      "selectionId": 1,
      "description: "XX"
    },
    ...
}, 
...
我使用的是Vue组件,它基本上保存JSON文件。在模板中,我有代码,所以当'defaultedOptionId===selectionId'时,作为默认值选中

<p v-for="(selection, index) in choice.selections">
     <input v-if="choice.defaultOptionId === selection.selectionId" type="radio" :name="'option_' + choice.choiceId" :value="selection.selectionId" checked></input>
     <input v-else type="radio" :name="'option_' + choice.choiceId" :value="selection.selectionId"></input>
        {{ index+1 }}. {{ selection.description }}

{{index+1}}。{{selection.description}}

“choice”基本上是JSON对象

如果我将defaultOptionId更改为其他内容,则此选项有效但是,如果我想在用户选择单选按钮时更新JSON对象,我必须向输入字段添加v-model,如下所示:

<p v-for="(selection, index) in choice.selections">
     <input v-if="choice.defaultOptionId === selection.selectionId" type="radio" :name="'option_' + choice.choiceId" :value="selection.selectionId" v-model="choice.selectedOptionId" checked></input>
     <input v-else type="radio" :name="'option_' + choice.choiceId" :value="selection.selectionId" v-model="choice.selectedOptionId"></input>
        {{ index+1 }}. {{ selection.description }}

{{index+1}}。{{selection.description}}

在这种情况下,具有“checked”属性的单选按钮不起作用。它检查第一个选项,即使我更改“defaultOptionId”,它也不在乎


基本上,我想用不同的“defaultOptionId”加载不同的JSON文件,这会更改单选按钮的默认检查,而当用户选择单选按钮时,它会更新JSON文件的“selectedOptionId”。我可以让它单独工作,但不能一起工作。我必须使用此设计,因为其他模板都采用相同的设计。

很难理解您试图做什么以及问题是什么,因为问题看起来很混乱,但我还是会尽力帮助您

演示: 下面是一个使用您的数据的工作示例:

模板: 我简化了JSON对象,因为它不需要其他属性

如果您的任务仍有困难,请告诉我

<div id="app">
  <div v-for="(selection, index) in choice.selections">
    <input 
      type="radio" 
      :id="selection.id" 
      :value="selection.selectionId" 
      v-model="choice.selected"
    >
    <label>
      {{selection.description}}
    </label>
  </div>
  
  <div>
    Selected: {{ choice.selected }}
  </div>
</div>
choice: { 
  "id": 1,
  "selected": 2,
  "selections": [
    {
      "selectionId": 1,
      "description": "Radio One"
    },
    {
      "selectionId": 2,
      "description": "Radio Two"
    }
  ]
}