我有PHP代码,我确实有一些变量
$checked = true;
我正在写这样的东西
<input v-model="subscribed" type="checkbox" <?= ($checked ? 'checked' : '') ?> />
现在在我的vue js文件中,我如何用这些判断过的数据填充我的模型
subscribed
有正确或错误的价值.(I don't intend to assign value to js directly by PHP code I need to set it inside/in input it self not anywhere else)
请判断代码片段.
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/vue@2.1.10/dist/vue.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="app">
<label>
<input id='sub' v-model="subscribed" @click="printState" type="checkbox" checked />
Subscription
</label>
</div>
<script>
new Vue({
el: '#app',
data: function() {
return {
subscribed: false,
// subscribed: sub.checked
// ^ this works but i dont want to add everytime id and get its value manually
}
},
created: function() {
var self = this;
setTimeout(function() {
console.log('Checkbox is having its initial value true/checked');
console.log(self.subscribed);
}, 1000)
},
methods: {
printState: function(){
console.log(this.subscribed)
}
}
});
</script>
</body>
您现在可以看到I have to add model variable in data 100 but then I also need to provide its value and if I provide its value then it will override its actual value(为了让它工作,我正在设置它最初从DOM元素中的值,它本身{sub.checked
sub是DOM.getelembyid获取的DOM元素…}但我不喜欢这样,因为会有更多的元素(文本| Select |广播)等等)
那么,有没有什么方法(优雅的方法)让subscribed
自动从输入中 Select 它的值呢.