如何将此选项添加到此图像选择器项目?

时间:2015-01-29 16:27:49

标签: javascript jquery

我有问题。这个概念是我有4个可点击的图像由http://rvera.github.io/image-picker/创建,我想创建一个选项,在单击按钮并选择适当的图像后,将某人转发到适当的href链接。例如,如果我用cat然后按钮点击图像,它会转发我关于猫的页面。如果我用狗和按钮点击图像,它会转发我关于狗的页面,如果我点击图片与狗和猫和按钮,它会转发我关于狗和猫的页面。

<html>

<head>
<link rel="stylesheet" href="css.css">
<script src="js.js"></script>



<!-- scripts for imagepicker -->
    <link rel="stylesheet" href="image-picker.css">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="image-picker.js"></script>
    <script type="text/javascript" src="image-picker.min.js"></script>



</head>
<body>


<!--code for imagepicker-->
<select multiple="multiple" class="image-picker show-html">
  <option data-img-src="http://placekitten.com/220/200" href="http://cats.com" value="1">Cute Kitten 1</option>
  <option data-img-src="http://placekitten.com/180/200" href="http://dogs.com" value="2">Cute Kitten 2</option>
  <option data-img-src="http://placekitten.com/130/200" href="http://dogsandcats.com" value="3">Cute Kitten 3</option>

</select>

<button href="#">Forward</button>


<script>

$("select").imagepicker()

</script>
</body>


</html>

我该怎么做?我完全不知道:/。

1 个答案:

答案 0 :(得分:1)

告诉我这是否有效。我添加了一个脚本,当您更改选择时,会更改值。

<!DOCTYPE HTML>
<html>

<head>
<link rel="stylesheet" href="css.css">
<script src="js.js"></script>



<!-- scripts for imagepicker -->
    <link rel="stylesheet" href="image-picker.css">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="image-picker.js"></script>
    <script type="text/javascript" src="image-picker.min.js"></script>
    <script type="text/javascript">
        "use strict";
        //wait for the page to be fully loaded
        window.onload = function() {
            initialize();
        }
    </script>



</head>
<body>


<!--code for imagepicker-->
<select multiple="multiple" class="image-picker show-html" id="selectImg">
  <option data-img-src="http://placekitten.com/220/200" value="http://cats.com">Cute Kitten 1</option>
  <option data-img-src="http://placekitten.com/180/200" value="http://dogs.com">Cute Kitten 2</option>
  <option data-img-src="http://placekitten.com/130/200" value="http://dogsandcats.com">Cute Kitten 3</option>

</select>

<button href="#" id="forwardButton">Forward</button>

<script>

function initialize() {

    var choice = document.getElementById("selectImg").value;

    //changes destination when you set or change your choice
    document.getElementById("selectImg").onchange = function() {
        choice = document.getElementById("selectImg").value;
    }

    //when button is clicked
    document.getElementById("forwardButton").onclick = function() {
        if (choice !== ("" || "undefined")) {
            window.location = choice;
        }       
    }
}

</script>
</body>
</html>
相关问题