简体   繁体   English

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

[英]can I bind to other input attribute besides value attribute in vuejs

I'm new to frontend javascript frameworks.我是前端 javascript 框架的新手。 Currently working on a piece of code and I'm curios if it's possible to bind some other input attribute than value to v-model in VueJS.目前正在处理一段代码,如果可以在 VueJS 中将除value之外的其他输入属性绑定到v-model ,我很想知道。 Here's what I mean:这就是我的意思:

In my html I have input like so:在我的 html 中,我输入如下:

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

And then later I want checked items to be shown by their names ( $label->name ) but I still need their values to be their ids for the backend.然后稍后我希望通过它们的名称( $label->name )显示检查的项目,但我仍然需要它们的值作为后端的 id。

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

This of course returns id of the item but I need name.这当然会返回项目的 ID,但我需要名称。 Is this possible?这可能吗?

You can use true-value and false-value :您可以使用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 -->
>

Excerpt from the 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. true-value 和 false-value 属性不会影响输入的 value 属性,因为浏览器不会在表单提交中包含未选中的框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM