2017-04-18 38 views
0

如何在下面的代碼中獲得方法selectChange中的row.question_idoption.AnswerMasterID的值?我正在使用Vue.js v2。將值發送到VueJS中的方法2

<div v-if="row.answer_input_type === 'Dropdown'"> 
    <template v-for="answer in answers"> 
    <template v-if="answer.AnswerTypeID === row.answer_type_id"> 
     <select class="dropdown_cl" v-bind:disabled="row.is_enabled == 1 ? false : true" @change="selectChange(row.question_id, answer.AnswerDescription)"> 
     <option v-for="option in answer.AnswerDescription" v-bind:value="option.AnswerMasterID" >{{option.AnswerDescription}}</option> 
     </select> 
     <p v-for="option in answer.AnswerDescription">{{option.AnswerMasterID}}</p> 
    </template> 
    </template> 
</div> 

我的方法如下:

selectChange: function (question_id, ans_master_id) { 
    alert(question_id + " " + ans_master_id) 
}, 

我想要得到的option.AnswerMasterID的值,它是在內部循環。

+0

什麼你到底是什麼意思?由於您將2個參數傳遞給了'selectChange'方法,因此您應該允許您的方法接受它們,並在記錄方法被觸發時傳遞的數據。 –

+0

看起來沒問題。有什麼問題嗎? – wostex

+0

我想要option.AnswerMasterID – sm12

回答

1

使用v-model指令中選擇元素的數據屬性綁定到它:

<select class="dropdown_cl" v-model="selectedID" ... 

請務必添加selectedID的數據屬性:

data() { 
    return { 
    selectedID: null, 
    } 
} 

既然你已經綁定option.AnswerMasterID作爲每個選項的值,selectedID將被綁定到所選選項的AnswerMasterID

然後,您可以參考像這樣的價值在您的selectChange方法:

selectChange: function (question_id) { 
    alert(question_id + " " + this.selectedID); 
}, 

Here is Vue's documentation for select input binding.

+0

這種工作,但我有不同的值,每個選擇和每個的第一個選項值是不同的。所以如果我寫選中:''和v-model =「selected」,那麼我將變得空白,作爲所有選擇元素的選定元素。如何解決這個問題? – sm12

+0

@sagarmajumdar這聽起來像一個不同的問題,我認爲你已經發布了? – thanksd