如何在下拉框中选择值

时间:2015-07-16 06:07:06

标签: javascript

这是我的代码。 如果我选择tamilnadu,我必须得到chennai。 如果我选择kerla我必须得到kochi。 如果我提交,如何获取值。

enter code here
<td align='center' >STATE:</td>
        <td> <select name ='state' onchange="showData()">
            <option value="0">Select</option>
            <option value="TAMILNADU">TAMILNADU</option>
            <option value="KERLA">KERLA</option>
            <option value="KARNATAKA">KARNATAKA</option>
            </td>

    </tr>
    <tr> <td>&nbsp;</td> </tr>
    <tr>
        <td align='center' name ='city'>city:</td>
        <td> <select>
        <option id='chennai'>chennai</option>
        <option id='kochi'>kochi</option>
        <option id='banglore'>banglore<option>
    </tr>

1 个答案:

答案 0 :(得分:0)

您可以使用正确的标记形式:

<强>的jQuery

$('#state').change(function() {
  $('#res').html($('#city option').eq($('#state option:selected').index()).val())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<tr>
  <td align='center'>STATE:</td>
  <td>
    <select name='state' id='state'>
      <option value="TAMILNADU">TAMILNADU</option>
      <option value="KERLA">KERLA</option>
      <option value="KARNATAKA">KARNATAKA</option>
    </select>
  </td>

</tr>
<tr>
  <td>&nbsp;</td>
</tr>
<tr>
  <td align='center' name='city'>city:</td>
  <td>
    <select name='city' id='city'>
      <option id='chennai'>chennai</option>
      <option id='kochi'>kochi</option>
      <option id='banglore'>banglore
        <option>
    </select>
</tr>
<br>
<br>RESULT : <span id='res'></span>

<强>使用Javascript:

function listResult() {
  var e = document.getElementById("state");
  document.getElementById("res").innerHTML = document.getElementById("city").options[e.selectedIndex].value


}
<tr>
  <td align='center'>STATE:</td>
  <td>
    <select name='state' id='state' onchange='listResult()'>
      <option value="TAMILNADU">TAMILNADU</option>
      <option value="KERLA">KERLA</option>
      <option value="KARNATAKA">KARNATAKA</option>
    </select>
  </td>

</tr>
<tr>
  <td>&nbsp;</td>
</tr>
<tr>
  <td align='center' name='city'>city:</td>
  <td>
    <select name='city' id='city'>
      <option id='chennai'>chennai</option>
      <option id='kochi'>kochi</option>
      <option id='banglore'>banglore
        <option>
    </select>
</tr>
<br>
<br>RESULT : <span id='res'></span>