duanpu2272 2018-08-09 20:24
浏览 45

选择第二个值

I have this select in my form:

<select id="select" name="gametype" onchange="myFunction(event)">
  <option disabled selected value>Please Select Game Type</option>
  <option value="28,000" >HD 6 Cameras saterday</option>
  <option value="30,000" >HD 6 Cameras</option>
  <option value="50,0000">HD 7 Cameras saterday</option>
  <option value="32,0000">HD 7 Cameras</option>
  <option value="45,000" >4k 7 Cameras saterday</option>
  <option value="32,000" >4k 7 Cameras</option>
</select>

I am passing the value of the option in $_POST. How can I also pass the text of the option? For example, if the value is "28,800" I also want to pass "HD 6 Cameras saterday."

  • 写回答

1条回答 默认 最新

  • dtjxhf595733 2018-08-09 20:54
    关注

    Not sure how you're handling the other components of this form, but I removed the onchange attribute binding from the select element and just used jquery to set the text displayed to the user as a hidden variable.

    <?php
      if(isset($_POST['gametype'])) {
        var_dump($_POST);
        echo "<hr/>";
      }
     ?>
    <script
      src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
      integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
      crossorigin="anonymous"></script>
    
      <form class="myForm" method="post">
        <!--this is empty and stays that way until you submit it-->
        <input type="hidden" name="gametype_desc" class="gametype_desc" value="" />
        <select id="select" class="selGameType" name="gametype">
          <option disabled selected value>Please Select Game Type</option>
          <option value="28,000" >HD 6 Cameras saterday</option>
          <option value="30,000" >HD 6 Cameras</option>
          <option value="50,0000">HD 7 Cameras saterday</option>
          <option value="32,0000">HD 7 Cameras</option>
          <option value="45,000" >4k 7 Cameras saterday</option>
          <option value="32,000" >4k 7 Cameras</option>
        </select>
      </form>
    
      <script>
        $(function(){
          $("select.selGameType").change(function(){
            //uncomment to log the value in the console
            //console.log($(this).children(":selected").text());
            //uncomment to log the value in the console
            //console.log($(this).val());
            //get the text of the currently selected option
            selText = $(this).children(":selected").text();
            //we set the value of the hidden variable, so that the text (not value) is passed as a separate post var
            $("input.gametype_desc").val(selText);
    
            //now you might submit it, or whatever your next step is...
            $("form.myForm").submit();
          })
        })
    
      </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100