javascript 流星从复选框 switchChange 中检索真/假值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27692259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
meteor retrieve true/false value from checkbox switchChange
提问by meteorBuzz
I have an event handler attached to checkboxes. I am using bootstrap switch http://www.bootstrap-switch.org/
我有一个附加到复选框的事件处理程序。我正在使用引导程序开关http://www.bootstrap-switch.org/
I am trying to get the state value which is either true or false into a variable so I can set it to the session value.
我试图将真或假的状态值放入变量中,以便我可以将其设置为会话值。
I can get the true or false value when the state changes (as seen in the rendered code). However, I would like to get this value within events. Please check my x variable.
当状态发生变化时,我可以获得 true 或 false 值(如呈现的代码所示)。但是,我想在事件中获得这个值。请检查我的 x 变量。
client.js
客户端.js
Template.myTemplate.rendered = function () {
$('input[name="questions"]').bootstrapSwitch('state', true, true);
$('input[name="questions"]').on('switchChange.bootstrapSwitch', function(event, state) {
console.log(state); // true | false
});
html:
html:
<template name="myTemplate">
<div class="row">
<input type="checkbox" name="questions" unchecked> Hobby?
</div>
<div class="row">
<input type="checkbox" name="questions" unchecked> Writer?
</div>
I am trying to get the value which is printed out in console.log(state)
within the rendered code into a variable within my event so I can set the session to value to either true or false.
我试图将console.log(state)
在渲染代码中打印出来的值放入我的事件中的变量中,以便我可以将会话的值设置为 true 或 false。
Template.myTemplate.events({
'click input': function(event) {
var x = $(this).is(":checked").val();
Session.set("statevalue", x);
console.log(Session.get("statevalue")); // this is not printing out anything
}
});
采纳答案by juliancwirko
You should use template instance:
您应该使用模板实例:
Template.myTemplate.events({
'click input': function(event, template) {
var x = template.$('input').is(":checked").val();
Session.set("statevalue", x);
console.log(Session.get("statevalue"));
}
});
or maybe with 'target':
或者可能与“目标”:
Template.myTemplate.events({
'click input': function(event) {
var x = $(event.target).is(":checked").val();
Session.set("statevalue", x);
console.log(Session.get("statevalue"));
}
});
回答by Pawe? Smo?ka
I think I have got a lot better sollution and not directly using JQuery:
我想我有更好的解决方案,而不是直接使用 JQuery:
Template.myTemplate.events({
'change input': function(event) {
var x = event.target.checked;
Session.set("statevalue", x);
console.log(Session.get("statevalue"));
}
});
Take care to see that I suggest you to use change instead of click in event.
请注意,我建议您使用更改而不是单击事件。
回答by user3410842
If you define id for your checkboxes, you could use
如果您为复选框定义 id,则可以使用
event.target.checkboxID.checked
with in Template-event.
在模板事件中。
this will return true or false if checked or not.
如果选中与否,这将返回 true 或 false。
回答by meteorBuzz
Template.myTemplate.rendered = function () {
$('input[name="questions"]').bootstrapSwitch('state', true, true);
$('input[name="questions"]').on('switchChange.bootstrapSwitch', function(event, state) {
var x = $(event.target).is(":checked");
Session.set("statevalue", x);
console.log(Session.get("statevalue"));
});
}
回答by Andy
'click .task_checkbox'(event){
const target = event.target;
var isChecked = $("#"+event.target.id).is(":checked").val();
console.log(isChecked);
},
回答by Saeed D.
The event
object already has all the information needed.
该event
对象已经拥有所需的所有信息。
Template.myTemplate.events({
'click input': function(event) {
if (event.target.name === 'questions') {
Session.set("statevalue", event.target.checked);
console.log(Session.get("statevalue"));
}
}
});