Correct display of user edit form (CRUD model) using checkbox
I am making a simple CRUD model with an input checkbox. I have no server side issues, everything is fine. I am using NodeJS + MongoDB. But I have a problem editing an existing user. When I edit an existing user with the checkbox checked (I get a JSON object with a parameter checked=true
), how do I display it using JS?
This is part of my users.js file in / routes / folder
var express = require('express');
var router = express.Router();
var User = require('../../models/User');
var rest = require('restler');
router.get('/adduser', function(req, res){
var user = new User();
user.contacts.push({phone: '', email: ''});
rest.get('http://localhost:3000/api/adduser').on('complete', function(data) {
res.render('users/add', { title: 'Add New Users' , n: user});
});
});
And this views / users / fields.jade part of the file for better understanding:
.form-group
label.col-sm-2.control-label(for='email') E-mail:
.col-sm-10
input(type="email", placeholder="email", name="email", value = n.contacts[0].email,required)
.form-group
.col-sm-offset-2.col-sm-10
input#enabled(type="checkbox",style='text-align: center; vertical-align: middle;',placeholder="", name="enabled", value = n.enabled)
| Enable user
So my problem is that I don't understand how I am supposed to show that the checkbox is actually checked when loading an existing user.
If the user is marked with the attribute n.enabled=true
, and if not n.enabled=false
. So if a user is verified to load that user, I need the input file to be verified.
I tried it in the following way, but he wrote to me that n is undefined ... and I don't know how to pass n as a parameter to this function:
$(document).ready(function(){
if(n.enabled=="true"){$("enabled").toggle(this.checked);}
});
source to share
Use #
for id-selectors
and use n.enabled
directly to hide or show your element, for example
$("#enabled").toggle(n.enabled);
//-^ prepend # before id selectors
toggle () will show / hide your element. To uncheck the checkbox use prop () like
$(document).ready(function(){
$("#enabled").prop("checked",n.enabled);
});
source to share