jQuery Select -> Скрыть / Показать div на выделении - PullRequest
0 голосов
/ 07 ноября 2011

У меня есть страница с множеством различных форм ... каждая из этих форм имеет поле выбора.После выбора одного из вариантов я хочу отобразить div.Когда любой из других вариантов выбран, я хочу скрыть тот же div.Однако у меня возникают проблемы с его запуском.

JQuery:

$('select').change(function() {

    daytimestamp = $('select').attr('id');

    val = $('select option:selected').val();

    alert(daytimestamp);

    alert(val);

    if( val == 'Other Event' ) {

        // We need to show the description div

        $('#event_description_' + daytimestamp).show();

    }

    else {

        // We need to hide the description div

        $('#event_description_' + daytimestamp).hide();

    }

});

Часть HTML:

<form name='add_cal_event_1320105600' id='add_cal_event_1320105600' action='' method='POST'>
<input type='hidden' name='event_timestamp' value='1320105600' />
Title: <input type='text' name='cc_title' width='100%' /><br />
Time: <input type='text' name='cc_time' size='8' /> <br />
Event type: <select name='cc_event_type' id='1320105600'>
<option value='Wedding'>Wedding
<option value='Rehearsal Dinner'>Rehearsal Dinner
<option value='Other Event'>Other Event
<option value='Event Pending'>Event Pending
</select>
<div id='event_description_1320105600' style='display:none;'>Event Title: <input type='text' name='cc_event_type_override' value='' /><br />Event Description: <input type='text' name='cc_event_description' value='' /></div>
Location: <input type='text' name='cc_location' /><br />
<textarea name='cc_description' class='mceEditor' cols='35'></textarea><br />
<input type='submit' onclick='javascript: jQuery("#add_cal_event_1320105600").submit();' name='submit_form' value='Add Event' />
</form>

Final jQuery (работает):

$(document).ready(function(){

    $('select').change(function() {

        daytimestamp = $(this).attr('id');

        val = $('#' + daytimestamp + ' option:selected').val();

        if( val == 'Other Event' ) {

            // We need to show the description div

            $('#event_description_' + daytimestamp).show();

        }

        else {

            // We need to hide the description div

            $('#event_description_' + daytimestamp).hide();

        }

    });

});

Ответы [ 2 ]

1 голос
/ 07 ноября 2011

попробуйте обернуть ваш код в

$(document).ready(function(){});
0 голосов
/ 07 ноября 2011

заменить 2-ю и 3-ю строку

daytimestamp = $ ('select'). Attr ('id');

val = $ ('select option: selected').val ();

с

daytimestamp = $ (this) .attr ('id');

val = $ (this).find ('option: selected'). val ();

...