Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.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/jquery上更改图像_Javascript_Jquery_Html - Fatal编程技术网

在下拉javascript/jquery上更改图像

在下拉javascript/jquery上更改图像,javascript,jquery,html,Javascript,Jquery,Html,我试图复制,但我的生活不能让它工作。第一张图片显示出来,但它永远不会改变。原谅我,如果这是基本的,我是相当新的javascript <html> <head> <script src="/js/jquery-1.11.2.js"></script> <script type="text/javascript"> var pictureList = [ "http://lorempixel.com/

我试图复制,但我的生活不能让它工作。第一张图片显示出来,但它永远不会改变。原谅我,如果这是基本的,我是相当新的javascript

<html>
<head>
    <script src="/js/jquery-1.11.2.js"></script>
    <script type="text/javascript">
    var pictureList = [
        "http://lorempixel.com/400/200/sports/1",
        "http://lorempixel.com/400/200/sports/2",
        "http://lorempixel.com/400/200/sports/3",
        "http://lorempixel.com/400/200/sports/4",
        "http://lorempixel.com/400/200/sports/5", ];

    $('#picDD').change(function () {
        var val = parseInt($('#picDD').val());
        $('img').attr("src",pictureList[val]);
    });

    </script>
</head>
<body>
    <img src="http://lorempixel.com/400/200/sports/1" />
    <select id="picDD">
        <option value="1" selected>Picute 1</option>
        <option value="2">Picute 2</option>
        <option value="3">Picute 3</option>
        <option value="4">Picute 4</option>
        <option value="5">Picute 5</option>
    </select>
</body>
</html>

var图片列表=[
"http://lorempixel.com/400/200/sports/1",
"http://lorempixel.com/400/200/sports/2",
"http://lorempixel.com/400/200/sports/3",
"http://lorempixel.com/400/200/sports/4",
"http://lorempixel.com/400/200/sports/5", ];
$('#picDD')。更改(函数(){
var val=parseInt($('#picDD').val());
$('img').attr(“src”,pictureList[val]);
});
皮库特1
皮库特2号
皮库特3
皮库特4
皮库特5

您需要将代码放入:

 // A $( document ).ready() block.
 $( document ).ready(function() {
    // Your code here.
 });
否则你的代码将无法工作。在jsfiddle中,您可以看到左侧的javascript是在domready上加载的

 $( document ).ready(function() {
    $('#picDD').change(function () {
        var val = parseInt($('#picDD').val());
        $('img').attr("src",pictureList[val]);
    });
});
在JSFIDLE中,您不必指定文档就绪函数,因为它是动态完成的。试试这个,它应该对你有用