0

When a [name="tip"] has the first value, display a specific element, otherwise it would display another element.

Both elements have the display:none property. When I load the page I check the select value and fadeIn the desired element automatically.

Everything works fine except when I try to change the selected option: nothing happens. I added both javascript onchange to that item and jquery .change() and nothing happens. Can you tell me why it does not work in my example?

<form id="fm-account-properties-tab" method="POST">
<table width="300px" style="margin-top:10px;" align="center" >

    <tr>
        
        <td align="left">
        <select class="easyui-combobox" name="tip" style="width:200px;" class="easyui-validatebox" required="true" onchange="changeType();">
                    <option value="l1">l1</option>
                    <option value="l2">l2</option>
                    <script>
                        $(document).ready(function(){
                            $('[name="tip"]').val('<?php echo $a['tip'];?>');
                        });
                    </script>
        </select>
        </td>
    </tr>
    <tr id="l1" style="display:none">

        <td align="left">
        <select class="easyui-combobox" name="l1"  style="width:200px;" class="easyui-validatebox" required="true">
                    
        </select>
        </td>
    </tr>
    <tr id="l2" style="display:none">

        <td align="left">
        <select class="easyui-combobox" name="l2"  style="width:200px;" class="easyui-validatebox">
                    
                    
        </select>
        </td>
    </tr>
</table>
</form>

<script>
function changeType()
{

if($('[name="tip"]').val()=='l1')
    {
    $('#l1').fadeIn();
    $('#l2').hide();
    }
else
    {
    $('#l2').fadeIn();
    $('#l1').hide();
    }
}



$(document).ready( function () {
        
        changeType();

            $('[name="tip"]').change(function(){ alert('1');});//it never alerts me
    });
MathieuAuclair
  • 1,229
  • 11
  • 41
Radu Vlad
  • 1,474
  • 2
  • 21
  • 37

3 Answers3

2

use .on for newer jQuery versions (jQuery > 1.7.x reference)

function changeType() {
    if ($('[name="tip"]').val() == '___') {
        $('#1').fadeIn();
        $('#2').hide();
    } else {
        $('#2').fadeIn();
        $('#1').hide();
    }
}

$(document).ready(function () {
    changeType();
    $('select[name="tip"]').on('change', function() {
        changeType();
    });
});

you have a double class attribute on your select.

here is a working sample http://jsfiddle.net/tvhKH/

Community
  • 1
  • 1
JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
0

May be you should use js error event to figure out the error in that code. E.g, <script> try {....all your codes here...} catch(err) {alert("THE ERRor" + err.message);} </script>. These code will alert your error and the exact point it occurs. Good luck!

Stanley Amos
  • 222
  • 2
  • 8
0

First, you should try to add a

console.log("It works");

or a

alert("It works")

in your method so you know if it is actualy called.

Then I think the issue may come from FadeIn() only works on elements with css {display: none} and visibility different of 'hidden', so are you sure your elements are ? In doubt, you may replace

.fadeIn();

by

.css('visibility','visible').hide().fadeIn(); // hide() does the same as display: none

Then please let me know if you have more information after doing that.

Ricola3D
  • 2,402
  • 17
  • 16