答案 0 :(得分:2)
您有一些失误,有些他们是:
1)首先,我建议将ids
添加到您的html元素中,以便更容易从Jquery
定位它们。
2) change
事件侦听器应该位于selects
之上,而不是form
之上。
3)您需要将values
属性添加到您的选项中...
因此,考虑到以前的评论,我重新组织了您的代码,如下所示:
$("#sel1, #sel2").on('change', function(e)
{
var firstSel = $('#sel1').val();
var secondSel = $('#sel2').val();
if (firstSel == 1 && secondSel == 1)
$("#print_html_here").html("You have selected English Girls.");
else if (firstSel == 2 && secondSel == 1)
$("#print_html_here").html("You have selected French Girls.");
else if (firstSel == 1 && secondSel == 2)
$("#print_html_here").html("You have selected English Boys.");
else if (firstSel == 2 && secondSel == 2)
$("#print_html_here").html("You have selected French Boys.");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="form">
<select id="sel1" name="firstselect">
<option value="1">English</option>
<option value="2">French</option>
</select>
<select id="sel2" name="secondselect">
<option value="1">Girls</option>
<option value="2">Boys</option>
</select>
</div>
<div id="print_html_here"></div>