简体   繁体   English

使用jQuery PHP获取下拉列表的选定值

[英]Get selected value of a dropdown's using jquery php

I have a drop down 我有一个下拉

  <div class="form-group">
 <select class="form-control" required name="uname" id="uname">
 <option value=""/>Select Your Name</option>
  <?php foreach ($this->getallusers as $users): ?>
 <option value="<?php echo $users['adminID'] ?>"<?= $users['adminID'] == $stories['Tm_id'] ? ' selected="selected"' : ''; ?>/><?php echo $users['UserName'] ?></option>
 <?php endforeach; ?>
</select>

Drop down have values.then when i am tried to get the selected drop down value from jquery. 下拉列表有values.then,当我试图从jQuery中获取选定的下拉列表值。

var op=$('#uname option :selected').text(); 
 var opid=$('#uname option :selected').val(); alert(opid);

When i alert op (text) i always got Select Your Name and opid is always blank.but drop down i have values pakka.Any help would be Appreciated . 当我提醒op (文本)时,我总是会得到“ 选择您的姓名”,并且opid始终为blank。但是在下拉菜单中,我有值pakka。任何帮助都将得到感谢。

try to use just this code: 尝试仅使用以下代码:

alert($("#uname").val());

//////// ////////

.val() - gets a value of any input and dropdown. .val()-获取任何输入和下拉列表的值。

Try below code to get the currently selected value and text when select box is change. 当更改select框时,请尝试以下代码来获取当前选定的值和文本。

$(document).ready(function(){
    $("select#uname").change(function(){
        var op = $(this).children("option:selected").text();
        var opid = $(this).children("option:selected").val();
        alert("You have selected - " + op + " -> " opid);
    });
});

Try removing the space from option and :selected on your jquery . 尝试从jquery option:selected删除空格。 So intead of 很有趣

var op=$('#uname option :selected').text(); 
var opid=$('#uname option :selected').val(); alert(opid);

it should be 它应该是

var op=$('#uname option:selected').text(); 
var opid=$('#uname option:selected').val(); alert(opid);

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

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