Help with 5th ed. book example 11.2.1 not working (conditional fields in forms)

99 views
Skip to first unread message

REM

unread,
Sep 6, 2013, 11:34:33 PM9/6/13
to web...@googlegroups.com

I am trying out the 11.2.1 book example on p.504, and it just doesn't function at all. I've tried it with Firefox and Chromium and the behavior is the same. Instead of the spouse name field appearing when the box is checked, nothing happens. I've checked the book code against the web book code against my code and they all appear to be the same.

Please help find my error!

Here's a cut and paste from the relevant book section:

As an example, create an input form that asks for a taxpayer's name and for the name of the taxpayer's spouse, but only if he/she is married.
Create a test application with the following model:



db
= DAL('sqlite://db.db')
db
.define_table('taxpayer',
   
Field('name'),
   
Field('married', 'boolean'),
   
Field('spouse_name'))


the following "default.py" controller:

def index():
    form
= SQLFORM(db.taxpayer)
   
if form.process().accepted:
        response
.flash = 'record inserted'
   
return dict(form=form)
 
and the following "default/index.html" view:

{{extend 'layout.html'}}
{{=form}}
<script>
jQuery
(document).ready(function(){
   jQuery
('#taxpayer_spouse_name__row').hide();
   jQuery
('#taxpayer_married').change(function(){
       
if(jQuery('#taxpayer_married').attr('checked'))
            jQuery
('#taxpayer_spouse_name__row').show();
       
else jQuery('#taxpayer_spouse_name__row').hide();});
});
</script>

The script in the view has the effect of hiding the row containing the spouse's name:


Unfortunately, in my tests, there's no effect. It certainly looks from the code as though the row should appear and disappear based on the checkbox, but it doesn't seem to be happening for me. Why doesn't it work?

Does it work for you?

Thanks.....


Alex

unread,
Sep 7, 2013, 4:33:30 AM9/7/13
to web...@googlegroups.com
write
if(jQuery('#taxpayer_married').is(':checked'))
instead of
if(jQuery('#taxpayer_married').attr('checked'))

attr returns the value as it is initially defined in html. With 'is' you are querying the current state.

Alex

Robert Moore

unread,
Sep 7, 2013, 12:23:24 PM9/7/13
to web...@googlegroups.com
Thanks a trillion!

I had started looking at the jQuery specifications and had immediately zeroed in on .attr but, because of my unfamiliarity with Javascript in general and jQuery in particular, I didn't see the particular issue just yet.

Your help is extremely appreciated. It's very difficult to learn from a book when the book contains errors, and I have spent the entire summer running around trying to get these book errors corrected when I find them.




--
 
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/UWUmUOP8ljE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages