0

I want to bind an index to the value attribute of the hidden field. I am trying this:

   <div id="panel" class="panel panel-default mt" v-for="(question,index) in questions.slice().reverse()">
<input type="hidden" v-model="question.question_no" :value="index">
</div>

it is showing me this error: enter image description here

what is the correct way to bind value attribute with an index?

2
  • Why do you need to do this? If question.question_no is not already set you shouldn't be doing it here, or it will already be set to the value you want. Also you should need to use hidden fields as the model will already have the data. Commented Mar 12, 2018 at 9:08
  • $question_no is set default to empty in the vue instance. i want to put hidden fields value (which is index) in the $question_no. how this will happen? Commented Mar 12, 2018 at 10:00

1 Answer 1

2

V-model is essentially syntax sugar for updating data on user input events, plus special care for some edge cases.

So

<input type="hidden" v-model="question.question_no">

is same as

<input type="hidden" :value="index" @input="question.question_no=$event.target.question.question_no">

So you can't use both the syntax together. Either use first one or second one according to your use case.

Edit

As input type is hidden

<input type="hidden" :value="index"> should be enough.

Reference

Sign up to request clarification or add additional context in comments.

7 Comments

Since the type of input is hidden it will not be rendered and so cant be edited. So there is no need of two way binding using v-model…+1:-)
yeah agreed. I forgot to see the input type. Edited answer. Thanks.
@ShubhamPatel $question_no is set default to empty in the vue instance. i want to put hidden fields value (which is index) in the $question_no. how this will happen?
You meant you need to put this index inside the model. Right?
You can't do this using template. what you need to do is after loading the questions data property. Write a function that preprocess the data then returns the processed data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.