Javascript 使用JS连接下拉列表中的值时出错
我已经编写了HTML和JS来连接下拉列表中的值。但我无法从中得到结果 代码如下:Javascript 使用JS连接下拉列表中的值时出错,javascript,html,Javascript,Html,我已经编写了HTML和JS来连接下拉列表中的值。但我无法从中得到结果 代码如下: jQuery获取所选选项值 $(文档).ready(函数(){ $(“.country”,“.state”).on('change',function(){ var selectedCountry=$(“.country”).find(“选项:selected”).val(); var selectedState=$(“.state”).find(“选项:selected”).val(); document.g
jQuery获取所选选项值
$(文档).ready(函数(){
$(“.country”,“.state”).on('change',function(){
var selectedCountry=$(“.country”).find(“选项:selected”).val();
var selectedState=$(“.state”).find(“选项:selected”).val();
document.getElementById('demo').innerHTML=selectedCountry+“”+selectedState;
});
});
选择国家:
美国
印度
大不列颠联合王国
华盛顿
新德里
伦敦
Eventhandler的选择器不正确,您可以通过$(selectboxSelector).val()轻松获取select box的值
您的选择器错误,应该是
$(“.country.state”)
$(document).ready(function(){
$('.country, .state').on('change',function(){
var selectedCountry = $(".country").val();
var selectedState = $(".state").val();
$('#demo').html( selectedCountry + " " + selectedState );
});
});