简体   繁体   English

meteor从checkbox switchChange中检索true / false值

[英]meteor retrieve true/false value from checkbox switchChange

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. 我试图将状态值(true或false)输入变量,因此我可以将其设置为会话值。

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. 请检查我的x变量。

client.js 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: 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
 }
});

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. 请注意,我建议您使用更改而不是单击事件。

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。

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"));
 }
});
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"));
}); 
}
'click .task_checkbox'(event){

    const target = event.target;
    var isChecked = $("#"+event.target.id).is(":checked").val();
    console.log(isChecked);

},

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM