admin管理员组文章数量:1200755
I have an event handler attached to checkboxes. I am using bootstrap switch /
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
}
});
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
}
});
Share
Improve this question
asked Dec 29, 2014 at 16:12
meteorBuzzmeteorBuzz
3,2005 gold badges36 silver badges62 bronze badges
6 Answers
Reset to default 15I think I have got a lot better sollution and not directly using 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
event.target.checkboxID.checked
with in Template-event.
this will return true or false if checked or not.
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.
Template.myTemplate.events({
'click input': function(event) {
if (event.target.name === 'questions') {
Session.set("statevalue", event.target.checked);
console.log(Session.get("statevalue"));
}
}
});
本文标签: javascriptmeteor retrieve truefalse value from checkbox switchChangeStack Overflow
版权声明:本文标题:javascript - meteor retrieve truefalse value from checkbox switchChange - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738543363a2095931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论