使用其 ID 时无法从选择框中检索数据


cant retrieve data from select box while using its id

这是我的选择标签,但是在编辑和添加时在网格中我无法检索国家/地区值,仅显示代码值 NW该怎么办?

<select name="country" id="country" >
<option value="" >select country</option>
<?php $select_query= mysql_query("Select * from country");
     while($select_query_array = mysql_fetch_array($select_query))
     {  
?>
<option value="<?php echo $select_query_array['code'];?>" <?php if($_POST['country']==$select_query_array['code']) { ?> selected="selected" <?php } ?> > <?php echo $select_query_array['country']; ?> </option>
<?php } ?>
</select>

例如:

<select name="test">
    <option value="1|Test one">Test one</option>
    <option value="2|Test two">Test two</option>
</select>

然后:

$test = explode('|', $_POST['test']);

然后你最终会得到$test[0]是"1",$test[1]是"测试一"。

我猜你正在使用jquery。

尝试这样的事情:

$('#country option:selected').text()

检查此解决方案:-

<?php $select_query= mysql_query("Select * from country"); ?>
<select name="country" id="country" >
<option value="" >select country</option>
<?php
 while($row = mysql_fetch_array($select_query))
  {
   if($_POST['country']==$select_query_array['code'])
    $selected='selected="selected"';
   else
    $selected='';
   echo '<option value="'.$row['code'].'"'.$selected.' >'.$row['country'].'</option>';
?>
</select>

首先删除你的 while 循环 仅获取数据,然后检查您的状况

<option value="<?php echo $select_query_array['code'];?>" <?=(isset($_POST['country']) && !empty($_POST['code']) && $_POST['country'] == $select_query_array['code']) ? $select_query_array['code'] : ""; ?> > <?php echo $select_query_array['code']; ?> </option>