dr2898 2016-06-06 15:02
浏览 54
已采纳

如何在条件存在时选择特定选项

I want to select the specific option in drop down list when a condition is existed.I set the Session in php and if the combo box has the value of 1 , it will be shown the option with value 1. I mean if session is 1, select the option with value of 1, if session is 2, select the option with vlaue of 2, and so on... . I want to set automatically select(I see the changes) with session in php. sth like blewo:

<select id="sel" >
  <option value='1'>one</option>
  <option value='2'>two</option>
  <option value='3'>three</option>
</select>
 <?php $_SESSION['num']='1'; ?>
<script>
  //must be shown the option with value of `1`.
<script>
  • 写回答

4条回答 默认 最新

  • dongzen2675 2016-06-06 15:17
    关注

    Try this .

    <select id="sel" >
      <option value='1'>one</option>
      <option value='2'>two</option>
      <option value='3'>three</option>
    </select>
     <?php $_SESSION['num']='1'; ?>
    <script>
    
        //set local variable value so that I don't have to do it for each of the following ways to do this.
        var num =  "<?php echo $_SESSION['num']; ?>";
    
        //normal javascript 
        document.getElementById("sel").value = num;
    
        //using jQuery  
        $("#sel").val(num);
    
    <script>
    

    Or this try vikingmaster's way

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度