当选择不同的选择选项时,更改另一个SELECT标记的值


Change value of another SELECT tag, when selecting different select options

我有一个选择标签,用户可以在这里选择他们想要的点击曝光类型:

 <select name="exposure">
                        <?php while($a = mysql_fetch_assoc($r)):  ?>
                            <option value=""><?php echo $a['exposure']; ?></value>
                        <?php endwhile; ?>
                        </select>

在它下面,我有另一个选择标签,它将显示点击,其值取自他们选择的曝光类型:

<select name="clicks">
                        <?php while($a = mysql_fetch_assoc($r)):  ?>
                            <option value=""><?php echo $a['amount']; ?></value>
                        <?php endwhile; ?>
                        </select>

我的问题是:我怎么能改变第二个选择标签的值,根据用户在第一个选择了什么?

这应该不会太难。你真正需要的是"曝光"值和"点击"值之间的映射,以及将"点击"值转换为有效标记的某种方法。您可以在jsFiddle上使用下面的示例:http://jsfiddle.net/ninjascript/5QPq5/

/**
 * Here's a map of possible values from 'exposure' and
 * related values for 'clicks'.
 */
var map = {
    "OptionA": ['A001','A002','A003'],
    "OptionB": ['B001','B002','B003']  
};
/** 
 * A quick and dirty method to turn some values in the map
 * into a set of <option> tags.
 */
function create(key)
{
    var result = [];
    if (map.hasOwnProperty(key)) {
        for (var i = 0; i < map[key].length; i++) {
            result.push('<option value="'+map[key][i]+'">');
            result.push(map[key][i]);
            result.push('</option>');
        }
    }
    return result.join('');
}
/**
 * Use jQuery to handle every 'change' event and add a new
 * set of <option> elements to the 'clicks' element.
 */
$('select[name="exposure"]').live('change', function()
{   
    var options = create(this.value);
    $('select[name="clicks"]').html(options);
    $('div').html('<span>foo</span>'); // <-- Update some other element too
});

编辑:jQuery.live()将在事件发生时执行函数。在这个函数中可以做任何想做的事情,包括更改多个DOM元素的内容。