简体   繁体   English

存储所选值和可靠下拉菜单的文本

[英]storing selected value and text of dependable dropdown menu

I am using dependable drop-down menu to populate the menu. 我正在使用可靠的下拉菜单来填充菜单。

First Menu is Country and second menu is city. 第一菜单是国家,第二菜单是城市。 On selecting the country it auto populates the city. 选择国家后,它将自动填充城市。

I am using this tutorial as reference. 我正在使用教程作为参考。

I want to store the value and text of selected city as i need it for displaying table based on this. 我想存储所选城市的值和文本,因为我需要它来基于此显示表。

My First menu is 我的第一个菜单是

Country :
<select name="country" class="country">
<option selected="selected">--Select Country--</option>
<?php
include('db.php');
$sql=mysql_query("select id,data from data where weight='1'");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$data=$row['data'];
echo '<option value="'.$id.'">'.$data.'</option>';
} ?>
</select> <br/><br/>

This is my second menu. 这是我的第二个菜单。 I want to store value of auto populated item. 我想存储自动填充项的值。 For this I have used on-change event in my drop-down of city menu as 为此,我在城市菜单的下拉菜单中使用了on-change事件作为

<select name="city" class="city" onchange="getselectval(this)">
<option selected="selected">--Select City--</option>
</select>

My getselectval() function is 我的getselectval()函数是

<script type="text/javascript">
 functionn getselectval(ctrl){
      selectval=ctrl.options[ctrl.selectedIndex].value;
      selecttext=ctrl.options[ctrl.selectedIndex].text;
      alert(selectval);
      alert(selecttext);

 }
<script>

After trying this script it alerts value of only selected item in city menu. 尝试此脚本后,它会提醒城市菜单中仅所选项目的值。 I am unable to alert the first item which is auto populated . 我无法提醒自动填充的第一项。 How can I achieve this ? 我该如何实现?

Any helps are Welcomed . 任何帮助都欢迎。

You can use jQuery here using below code - 您可以在此处使用以下代码使用jQuery-

remove on change function from select box select框中删除更改功能

<select name="city" class="city">
  <option selected="selected">--Select City--</option>
</select>

bind change event to select box using jQuery and get selected option text and value 使用jQuery将更改事件绑定到选择框,并获取选择的选项文本和值

$(function(){
  $('select[name=city]').on("change",function(){
   var value = $(this).val();
   var text = $(this).find('option:selected').text();
   alert(value);
   alert(text);
  });
});

Check this Code 检查此代码

only HTML get JS from JSfiddle 只有HTML从JSfiddle获取JS

<form id="dropdowns" action="index.html">
<label>Country:</label>
<select id="country" name="country">
    <option value="000">-Select Country-</option>
</select>
<br />
<label>State:</label>
<select id="state" name="network">
    <option value="000">-Select State-</option>
</select>
<br />
<label>City:</label>
<select id="model" name="model">
    <option value="000">-Select City-</option>
</select>
<br />

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM