简体   繁体   English

如何将我的选择选项分配给另一个变量

[英]How do I assign my select option to another variable

I need to keep track of what option I have selected 1 thru 5. I can't use the value option as I use that to store the image. 我需要跟踪选择了1到5的选项。我无法使用value选项来存储图像。 So I count the number of options. 因此,我计算了选项的数量。 The code below works great in Firefox, but seems like the script doesn't run under Chrome or Safari. 下面的代码在Firefox中效果很好,但是该脚本似乎无法在Chrome或Safari下运行。 Have not tried IE or edge yet. 尚未尝试IE或Edge。 Any Ideas please? 有什么想法吗? Example at: http://rtpcservices.com/count_options.php 例如: http//rtpcservices.com/count_options.php

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<span>Click a option!</span>

<select id="selected_image" name="selected_image" onchange="
$('#imageToSwap').attr('src', this.options[this.selectedIndex].value);">

<?php
echo '
    <option value="' .$shop_name_pdo . '/images/' . $p_image_1 . ' ">Image 1</option>
    <option value="' .$shop_name_pdo . '/images/' . $p_image_2 . ' ">Image 2</option>
    <option value="' .$shop_name_pdo . '/images/' . $p_image_3 . ' ">Image 3</option>
    <option value="' .$shop_name_pdo . '/images/' . $p_image_4 . ' ">Image 4</option>
    <option value="' .$shop_name_pdo . '/images/' . $p_image_5 . ' ">Image 5</option>
</select>
';
?>

<script>
$( "option" ).click(function() {
    // `this` is the DOM element that was clicked
    var index = $( "option" ).index( this );
    $( "span" ).text( "Image Selected_option #" + (index+1));
    document.getElementById("selected_option").value = index;    
});

</script> 

Use a change event 使用变更事件

$( "#selected_image" ).change(function() {
    var index = $( "option:selected" ).index();
    $( "span" ).text( "Image Selected_option #" + (index+1));
    $("#selected_option").val(index);
});

 $( "#selected_image" ).change(function() { var index = $( "option:selected" ).index(); $( "span" ).text( "Image Selected_option #" + (index+1)); $("#selected_option").val(index); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="selected_image" name="selected_image"> <option value="' .$shop_name_pdo . '/images/' . $p_image_1 . ' ">Image 1</option> <option value="' .$shop_name_pdo . '/images/' . $p_image_2 . ' ">Image 2</option> <option value="' .$shop_name_pdo . '/images/' . $p_image_3 . ' ">Image 3</option> <option value="' .$shop_name_pdo . '/images/' . $p_image_4 . ' ">Image 4</option> <option value="' .$shop_name_pdo . '/images/' . $p_image_5 . ' ">Image 5</option> </select> <span></span> <input type="text" id="selected_option"> 

Change your code to the following code try to change the following line 将您的代码更改为以下代码尝试更改以下行

var index = $( "option" ).index( this ); 

to

var index = $( "option:selected" ).index( this );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何禁用基于另一个选择选项的选择选项? - How do I disable a select option based on another select option? 如何根据 select 标签的选定选项的值为变量分配新值,检索并使用变量的新值? - How do I assign a new value to a variable based on the value of a selected option of a select tag, retrieve and use the new value of the variable? 如何获取 datalist 选项的值并将其分配给另一个元素? - How do I get the value of a datalist option and assign it to another element? 如何创建变量以跟踪我的html select选项 - How do I create a variable to keep track of my html select option 如何通过更改 html 表单中的选择选项来更改变量? - How do I change a variable with the change of a select option in an html form? 如何将.reduce函数的输出分配给变量? - How do I assign my .reduce function output to a variable? 我如何分配变量,如果 - how do I assign a variable if 如何从下拉列表中分配选项值以选择另一个下拉列表的ID? - How to assign the option value from a dropdown to select id of another dropdown? 如何将选择选项元素的值分配给另一个 div 子项 - How to assign value from select option elements to another div childs 如何使用Coldfusion 8将SELECT OPTION中的TEXT(不是VALUE)分配给变量? - How to assign the TEXT (not the VALUE) from a SELECT OPTION into a variable using Coldfusion 8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM