php下拉按钮被选中

时间:2013-04-22 12:25:14

标签: php javascript mysql

在表单编辑数据中希望以表格形式显示...但下拉值不应该被选中...我想要下拉值获取选择 这里是list_search函数中的代码id返回cmb_fruit,val返回Apple或Orange

              <tr>
                <td width="38%" height="28" align="left"><span class="style20"><font color="#DC143C">*</font>Profession</span></td>
                <td width="62%" align="left"><label for="profession"></label>
                  <select name="cmb_fruit" size="1" id="cmb_fruit">
                    <option value="Select">Select</option>
                    <option value="Apple">Apple</option>
                    <option value="Orange">Orange</option>
                  </select>
    <script language="javascript" type="text/javascript">
        list_search('cmb_fruit',<?php echo "'" . @mysql_result($rs_modify,0,'fruit') . "'";?>)
    </script>
                  </td>
              </tr>


function list_search(id,val)
 {
 cnt=document.getElementById(id).length
 for(x=0; x<cnt; x++ )
 {
     if( document.getElementById(id).options(x).value==val)
     {

         document.getElementById(id).options(x).selected=true
         break;
     }
 }
} 

2 个答案:

答案 0 :(得分:0)

var fruit = document.getElementById("fruit");
fruit.options[fruit.options.selectedIndex].selected = true;

答案 1 :(得分:0)

您可以设置选择的value属性。另外,如果要从正文中运行的脚本调用list_search,请确保head函数已声明。

function list_search(id, val) {
    document.getElementById(id).value = val;
}

请注意,您不需要使用javascript,并且可以简单地将selected属性放在要选择的option元素上。

<option value="Apple" selected>Apple</option>