【发布时间】:2018-08-09 17:04:34
【问题描述】:
我正在将表单中的 v-model 值分配给 api 中的参数。我必须将 api 中的一个参数分配给两个输入,即:name。 name 是必填字段。
使用每个输入的v-model 值,如果选择了第一个radiobtn,它将返回null 的值name。如果选择了第二个 radiobtn,用户必须在文本框中输入文本。
我需要检查第一个单选按钮是否已被点击或用户在文本框中输入文本并成功将第一个单选按钮或文本框的值传递给相同的 api 参数 (name)。
我怎样才能做到这一点?
HTML:
<label class="radiogrp"><input type="radio" v-model="picked" name="default_user" value="reg" >Mary</label>
<label class="radiogrp"><input type="radio" v-model="picked" name="new_usr" value="non-reg"><input type="text" v-model="new_user" ></label>
JS:
/* API parameters
name: (string) or (null)
*/
new Vue({
el: '#app',
data: {
picked: Boolean,
new_user: ""
},
/* Two v-model form values for the one property in the API. */
submit_name(){
this.$http.post("https://jsonplaceholder.typicode.com/users",{
name: this.picked || this.new_user})
// ....then() etc.
}
});
【问题讨论】:
标签: rest api vuejs2 submit radio-group