Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JS连接下拉列表中的值时出错_Javascript_Html - Fatal编程技术网

Javascript 使用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

我已经编写了HTML和JS来连接下拉列表中的值。但我无法从中得到结果

代码如下:


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 );
    });
});