除了vuejs中的值属性,我可以绑定到其他输入属性吗

can I bind to other input attribute besides value attribute in vuejs

我是前端 javascript 框架的新手。目前正在处理一段代码,我很好奇是否可以在 VueJS 中将 value 以外的其他输入属性绑定到 v-model。这就是我的意思:

在我的 html 中,我的输入是这样的:

<input type="checkbox" name="labels[]" id="label_{{$label->id}}" value="{{$label->id}} v-model="checked">

然后我希望选中的项目按名称显示 ($label->name),但我仍然需要将它们的值作为后端的 ID。

<div v-for="label in checked" >@{{ label }}</div>

这当然是 returns 项目的 ID,但我需要名称。这可能吗?

您可以使用 true-valuefalse-value:

<input 
 type="checkbox" 
 name="labels[]" 
 id="label_{{$label->id}}" 
 value="{{$label->id}}" 
 v-model="checked"
 true-value="{{$label->name}}"
 false-value="something else" 
 <!-- quote is not required inside true-value, false-value. 
  it will automatically set the type -->
>

摘自doc

The true-value and false-value attributes don’t affect the input’s value attribute, because browsers don’t include unchecked boxes in form submissions.