duanjian9148
2017-05-18 07:45
浏览 124

如何在文本框中显示下拉列表选定值

When I choose from the dropdown list, I want to get the selected value of the dropdown list and show in the . But I try to do with above code, it didn't work ? whats wrong on that, plz suggest me. Thanks

$('.assetgroupSelect').on('change', function () {
  var value = $('.assetgroupSelect').val();
  if (value != "") {            
    $('#idValue').text(value)
  }
}).trigger('change');
<?
  $select_query = "Select Group_ID, Group_Name from Asset_Group";
  global $DB;
  $arrResult = array();
  $err_mess = 'Test';
  $result = $DB->Query($select_query, false, $err_mess.__LINE__);
?>

<tr height="50">
  <td style="width: 220px">
    <label for="assetGroup">
      <span style="color:red">*</span>Asset Group:
    </label>
  </td>
  <td>
    <select id="assetgroupSelect" name ='assetgroupSelect'>
      <option value="">(choose asset group)</option>
      <? while ($arrResult = $result->Fetch()) { ?>
        <option value="<? echo $arrResult['Group_ID'] ?>"> <? echo $arrResult['Group_Name'] ?> </option>
      <? } ?>
    </select><br><br>
    <div class="placer"> 
      Current index is :
      <div id="idValue"></div>
    </div>
  </td>
</tr>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtpd58676 2017-05-18 07:53
    已采纳

    You just need to pass value on change

        <script>
    
        function myFunction(value) {
            $('#idValue').text(value)
        }
    
            </script>
    >>>>>>>>>> Drop Down List <<<<<<<<<<<<
    

    And in your php code you need onchange="myFunction(this)" in select tag

    <?
                            $select_query = "Select Group_ID, Group_Name from Asset_Group";
                            global $DB;
                            $arrResult = array();
                            $err_mess = 'Test';
                            $result = $DB->Query($select_query, false, $err_mess.__LINE__);
                            ?>
    
                        <tr  height="50">
                            <td style="width:220px"><label for="assetGroup"><span style="color:red">*</span>Asset Group:</label></td>
                            <td>
                                <select id="assetgroupSelect" name ='assetgroupSelect' onchange="myFunction(this)">
                                    <option value="">(choose asset group)</option>
                                    <?while ($arrResult = $result->Fetch()){?>
                                <option value = "<? echo $arrResult['Group_ID'] ?>"> <? echo $arrResult['Group_Name'] ?> </option>
                                    <?}?>
                                </select><br><br>
                                <div class="placer"> Current index is :
                                        <div id="idValue"></div>
                                </div>
                            </td>
                        </tr>
    

    Hope this will help you

    打赏 评论
  • douzhan8395 2017-05-18 07:48

    You use $('.assetgroupSelect') but you should use $('#assetgroupSelect') because it is (select id="assetgroupSelect"...) in HTML.

    And also do not forget tu use semicolon here at the end of line: $('#idValue').text(value)

    打赏 评论
  • duanjian4150 2017-05-18 08:00

    issue in .assetgroupSelectyou need us #assetgroupSelect' Because assetgroupSelect is ID not Class

    try it

    $('#assetgroupSelect').on('change', function () {
      var value = $('#assetgroupSelect').val();
      if (value != "") {            
        $('#idValue').text(value)
      }
    }).trigger('change');
    

    update Comment

    $('#assetgroupSelect').on('change', function () {
      var value = $('#assetgroupSelect').val();
      if (value != "") {            
        $('#idValue').text(value)
      }
    }).trigger('change');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <tr height="50">
      <td style="width: 220px">
        <label for="assetGroup">
          <span style="color:red">*</span>Asset Group:
        </label>
      </td>
      <td>
        <select id="assetgroupSelect" name ='assetgroupSelect'>
          <option value="">(choose asset group)</option>
         
          <option value="A"> A </option>
         <option value="B"> B </option>
         <option value="C"> C </option>
         <option value="G"> G </option>
         <option value="H"> H </option>
         <option value="I"> I </option>
         
        </select><br><br>
        <div class="placer"> 
          Current index is :
          <div id="idValue"></div>
        </div>
      </td>
    </tr>

    </div>
    
    打赏 评论

相关推荐 更多相似问题