我学习ruby(RoR 4.2.5,ruby 2.1.5),我需要做2个选择,第一个是针对用户而第二个是针对折扣
SELECT 1
<select id="select1">
<option value="1">Person1</option>
<option value="2">Person2</option>
<option value="3">Person3</option>
<option value="4">Person4</option>
</select>
SELECT 2
<select id="select2">
<option value="100">1</option>
<option value="200">2</option>
<option value="50">3</option>
<option value="20">4</option>
</select>
如果您选择&#34; Person1&#34; select2需要&#34; 1&#34;价值&#34; 100&#34;,&#34; Person2&#34; select2需要&#34; 2&#34;价值&#34; 200&#34;。 有什么想法吗?
答案 0 :(得分:0)
由于它们始终处于同步状态,因此您可以检查索引,然后相应地设置第二个索引。
var option_index = $("#select1 option:selected").index();
然后您可以根据第一个索引设置第二个。 $('#select2 option').eq(option_index).prop('selected', true);
当然你需要初始化然后再听取on change事件。
以下是JS的完整示例(使用 jQuery ):
//Initialize on ready:
var option_index = $("#select1 option:selected").index();
$('#select2 option').eq(option_index).prop('selected', true);
//Listen for the change
$('#select1').on('change', function(){
option_index = $("#select1 option:selected").index();
$('#select2 option').eq(option_index).prop('selected', true);
});