繁体   English   中英

jQuery选择->选择时隐藏/显示div

[英]jQuery Select -> Hide/Show div on selection

我的页面设置了很多不同的表格...每种表格都有一个选择字段。 选择其中一个选项后,我要显示一个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>

最终的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();

        }

    });

});

尝试将代码包装在

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

替换第二和第三行

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM