duangu8264 2018-06-01 17:12
浏览 56
已采纳

当通过select标签选择人数时获取输入值和数值

Please forgive me if i explain wrong. My website has different kinds of ticket name each ticket name has different price. I would like to do when I select the number through <select> tag it get the number value multiply by ticket price value. my code

<?php
session_start();
if (isset($_GET['id'])) {
    $id=$_GET['id'];
}
include("database/connection.php");
$sql1="SELECT title,GROUP_CONCAT(ticketname),GROUP_CONCAT(price) FROM `evprice` where title='$id'";
$q4=mysql_query($sql1);
$row1=mysql_fetch_array($q4);

?>
<form action="test.php" method="post">
 <div class="gaps">
   <div style="float: right; padding-right: 400px; ">
    <?php 
     $rowmain1=explode(',', $row1[2]);
     for ($i=0; $i<count($rowmain1); $i++) {
      echo "<label style='padding:5px;'>$rowmain1[$i]</label><br>";
    }
    ?>
  </div>
  <?php 
    $rowmain=explode(',', $row1[1]);
    foreach ($rowmain as $item) {
     echo"$item";
  ?>
 <select class="selectpicker selectcon" name="people" style="display: inline;" name ="count" value='$item'>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<?php
 echo "<br>";
}?>
</div>

that means enter image description here If I did mistakes please correct my code

  • 写回答

1条回答 默认 最新

  • doumi1912 2018-06-01 17:55
    关注

    For complement the answer an example could be: 1.- Adding jquery to your site.

    <script src="https://code.jquery.com/jquery-3.3.1.slim.js" integrity="sha256-fNXJFIlca05BIO2Y5zh1xrShK3ME+/lYZ0j+ChxX2DA="crossorigin="anonymous"></script>
    

    2.- Need to prepare your document, the next code will get the value of the select it self.

    <script type="text/javascript">
    $(document).on('ready', function(){
    $('select').on('change', function() {
      var selectValue = $(this).val();
      console.log(selectValue);
    });
    });
    </script>
    

    3.- You need to set on the iteration if the label an attribute like 'myattr="value"' with the value you want to mutiply. And get it and used it, need to add something like:

    echo "<label style='padding:5px;' id='".>$rowmain1[$i]."' myattr="value">$rowmain1[$i]</label><br>";
    
    <script type="text/javascript">
       $(document).on('ready', function(){
        $('select').on('change', function() {
          var selectValue = $(this).val();
          console.log(selectValue);
          var valueTicket = $("#my_id").attr("myattr");
          console.log(valueTicket);
          var mutiplyvalue = valueTicket * selectValue;
          console.log(mutiplyvalue);
        });
       });
        </script>
    

    Need to change on your needs. Good luck.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line