最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I pass multiple values in the <option> tag? - Stack Overflow

programmeradmin3浏览0评论

I have a program that simulates a folding effect on a flyer that has a front image and a back image. I select the flyers through a dropDownList. How can I pass the back image in the tag so that, when I rotate the image, the picture on the back is shown?

This is the HTML for my dropDownList:

<form name="Pictures">
        <select name="dropPic" onchange="selectFront(this.value)">
            <option value="Flyer1pag1.png" value="Flyer1pag2.png">Flyer 1</option>
            <option value="Flyer2pag1.png" value="Flyer1pag2.png">Flyer 2</option>
            <option value="Flyer3pag1.png" value="Flyer1pag2.png">Flyer 3</option>
        </select>
    </form>

I have a program that simulates a folding effect on a flyer that has a front image and a back image. I select the flyers through a dropDownList. How can I pass the back image in the tag so that, when I rotate the image, the picture on the back is shown?

This is the HTML for my dropDownList:

<form name="Pictures">
        <select name="dropPic" onchange="selectFront(this.value)">
            <option value="Flyer1pag1.png" value="Flyer1pag2.png">Flyer 1</option>
            <option value="Flyer2pag1.png" value="Flyer1pag2.png">Flyer 2</option>
            <option value="Flyer3pag1.png" value="Flyer1pag2.png">Flyer 3</option>
        </select>
    </form>

Here is the function in JavaScript that changes the front image regarding the selection of the dropDownList and the function that should take the back image of the flyer:

function selectFront(imgSrc) {
            loadImage(imgSrc);
            var Dim_Slice = document.querySelectorAll(".slice");
            for (var i = 0; i < Dim_Slice.length; i++) {
                Dim_Slice[i].style.backgroundImage = "url(" + imgSrc + ")";
            }
        }

function selectBack(imgSrc) {
            var Dim_Sliceb = document.querySelectorAll(".sliceb");
            for (var i = 0; i < Dim_Sliceb.length; i++) {
                Dim_Sliceb[i].style.backgroundImage = "url(" + imgSrc + ")";
            }
        }

Share Improve this question edited Nov 21, 2015 at 18:15 pnuts 59.5k11 gold badges91 silver badges141 bronze badges asked Jul 23, 2015 at 5:54 PikachuuPikachuu 1552 gold badges5 silver badges17 bronze badges 2
  • Did you consider adding hidden input with necessary data? Or maybe some custom attributes, like data-back-image? – mic4ael Commented Jul 23, 2015 at 6:05
  • Could you use data attributes instead? e.g. <option value="Flyer1" data-flyer-one="Flyer1Pag1.png" data-flyer-two="Flyer1pag2.png">Flyer 1</option> – jasonscript Commented Jul 23, 2015 at 6:06
Add a ment  | 

5 Answers 5

Reset to default 5

it can be done in this way

<select id="dropPic">
    <option data-picone="Flyer1pag1.png" data-pictwo="Flyer1pag2.png">Flyer 1</option>
    <option data-picone="Flyer2pag1.png" data-pictwo="Flyer1pag2.png">Flyer 2</option>
    <option data-picone="Flyer3pag1.png" data-pictwo="Flyer1pag2.png">Flyer 3</option>
</select>

And on change event you can get the value as below

$("#dropPic").change(function () {
     alert($(this).find(':selected').data('picone'));
}); 

Check this link for similar question

Can an Option in a Select tag carry multiple values?

Your question and / or what you're trying to do isn't entirely clear, but I think this is what you want:

<form name="Pictures">
  <select name="dropPic"
    onchange="selectFront(this.value.split(',')[0]);selectBack(this.value.split(',')[1]);">
    <option value="Flyer1pag1.png,Flyer1pag2.png">Flyer 1</option>
    <option value="Flyer2pag1.png,Flyer1pag2.png">Flyer 2</option>
    <option value="Flyer3pag1.png,Flyer1pag2.png">Flyer 3</option>
  </select>
</form>

You cannot have Multiple values in an Option Tag.

If the Name of the Front and Back-Image only differ by the Page-number, you can use this code:

function selectFlyer(name){
	selectFront(name+"pag1.png");
	selectBack(name+"pag2.png");
}
function selectFront(imgSrc) {
    loadImage(imgSrc);
    var Dim_Slice = document.querySelectorAll(".slice");
    for (var i = 0; i < Dim_Slice.length; i++) {
        Dim_Slice[i].style.backgroundImage = "url(" + imgSrc + ")";
    }
}


function selectBack(imgSrc) {
	loadImage(imgSrc);
    var Dim_Sliceb = document.querySelectorAll(".sliceb");
    for (var i = 0; i < Dim_Sliceb.length; i++) {
        Dim_Sliceb[i].style.backgroundImage = "url(" + imgSrc + ")";
    }
}
<form name="Pictures">
  <select name="dropPic" onchange="selectFlyer(this.value)">
    <option value="Flyer1">Flyer 1</option>
    <option value="Flyer2">Flyer 2</option>
    <option value="Flyer3">Flyer 3</option>
  </select>
</form>

You can feed it a series of values, separated by mas:

<select id="BLAH" onchange="readBlah()">
  <option value="1,car,red">1</option>
  <option value="2,car,red">2</option>
  <option value="3,truck,blue">3</option>
</select>

function readBlah() {
  var e = document.getElementById("BLAH");
  feedArray=e.value.split(","); 
  console.log(feedArray[0]); 
  console.log(feedArray[1]); 
  console.log(feedArray[2]); 
}
   <form method="post">            
    <select class='form-control' name= 'dropPic'>
        <option value="Flyer1pag1.png,Flyer1pag2.png">Flyer 1</option>
        <option value="Flyer2pag1.png,Flyer1pag2.png">Flyer 2</option>
        <option value="Flyer3pag1.png,Flyer1pag2.png">Flyer 3</option>
            </select>
    </form>




     <?php
                    $dropPic=$_POST['dropPic'];
                    $result=explode(',', $dropPic);
                    $dropPic1=$result[0];
                    $dropPic2=$result[1];
     ?>
发布评论

评论列表(0)

  1. 暂无评论