meteor从复选框switchChange中检索true / false值 [英] meteor retrieve true/false value from checkbox switchChange

查看:135
本文介绍了meteor从复选框switchChange中检索true / false值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个事件处理程序附加到复选框。我正在使用引导开关 http://www.bootstrap-switch.org/



我正在尝试将一个true或false的状态值转换为变量,以便我可以将其设置为会话值。



当状态发生变化时,我可以获得true或false值(如在渲染代码中所示)。但是,我想在事件中获得这个值。请检查我的x变量。



client.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:

 code>< template name =myTemplate> 
< div class =row>
< input type =checkboxname =questionsunchecked>爱好?
< / div>
< div class =row>
< input type =checkboxname =questionsunchecked>作家?
< / div>



我试图得到在代码中的 console.log(state)中打印出的值到我的事件中的变量中,所以我可以将会话设置为true或false。 / p>

  Template.myTemplate.events({
'click input':function(event){
var x = $(this).is(:checked)。val();
Session.set(statevalue,x);
console.log(Session.get(statevalue)) ; //这不是打印出任何东西
}
});


解决方案

您应该使用模板实例:

  Template.myTemplate.events({
'click input':function(event,template){
var x = template 。$('input')。is(:checked)。val();
Session.set(statevalue,x);
console.log(Session.get(statevalue ));
}
});

或者可能是target:

  Template.myTemplate.events({
'click input':function(event){
var x = $(event.target).is(:checked ).val();
Session.set(statevalue,x);
console.log(Session.get(statevalue));
}
} );


I have an event handler attached to checkboxes. I am using bootstrap switch 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.

client.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:

<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.

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
 }
});

解决方案

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"));
 }
});

这篇关于meteor从复选框switchChange中检索true / false值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆