Yii Framework Forum: Make Form Element Disappear Based On Dropdown Selection - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Make Form Element Disappear Based On Dropdown Selection Rate Topic: -----

#1 User is offline   gvanto 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 16-August 12

Posted 08 October 2012 - 02:41 AM

I have model create form (to create a new user) and I would like to make one of the form elements appear only when a certain item is selected in a dropdown select menu.

I think jquery is the best way probably but I've not found much on how to create jquery functions and including them using the yii framework.

This one seems to stop after a very very short introduction on how to get 'hello' alert working:
http://usingjquery.c...-yii-framework/


<?php
    Yii::app()->clientScript->registerScript('helloscript',"
        alert('hello');
    ",CClientScript::POS_READY);
?>


I need to create a function that will trigger when the dropdown select changes value, how to do this in yii?

any help much appreciated,
thanks
gvanto
0

#2 User is offline   alex-w 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 223
  • Joined: 25-November 10

Posted 08 October 2012 - 02:44 AM

This should help give you an idea,

$(function() {
  $('#mydropdown').on('change', function() {
    $('#otherfield').css('display', ('myval' === $(':selected', this)) ? 'block' : 'none');
  });
});

0

#3 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,168
  • Joined: 05-January 12
  • Location:Morocco

Posted 08 October 2012 - 04:08 AM

<?php
    Yii::app()->clientScript->registerScript('helloscript',"
        $(document).on('change', '#id-of-dropdown', function() {
            $('#if-of-element').toggle($(this).val() == dropdown_value_that_you_want_to_match);
        });
    ",CClientScript::POS_READY);
?>

1

#4 User is offline   ThePaulius 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 17-February 12

Posted 31 January 2013 - 08:02 AM

View Postbennouna, on 08 October 2012 - 04:08 AM, said:

<?php
    Yii::app()->clientScript->registerScript('helloscript',"
        $(document).on('change', '#id-of-dropdown', function() {
            $('#id-of-element').toggle($(this).val() == dropdown_value_that_you_want_to_match);
        });
    ",CClientScript::POS_READY);
?>



Thats really great, its works nicely, but i have 'bodged' a default hidden by adding style="display:none" to the DIV thats being toggled.. :-X

Also, if i then go back into the form, the div is hidden (obviously) to show it again to edit the value of the form element, I have to 'change' the drop down to 'toggle' the hidden div...

how can I set it to show the field if its been used, uhm, this sort of thing easier to explain in code if ( #id-of-dropdown == 'xyz') { show(); }

Or if #id-of-element !empty()...
0

#5 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,168
  • Joined: 05-January 12
  • Location:Morocco

Posted 01 February 2013 - 04:23 AM

Like this?

    $(document).on('change', '#id-of-dropdown', function() {
        if ($(this).val() == dropdown_value_that_you_want_to_match) {
            $('#id-of-element').show();
        } else {
            $('#id-of-element').hide();
        }
    });

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users