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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化