doumu5023 2019-06-05 10:33
浏览 92

包含jquery脚本还是在while循环中更改变量值?

Attempting to call a php variable as a span id, the variable changes with the value of $i in a for statement. I'm using a for statement to call a series of products from the database, no problems there. However when attempting to have the jquery run for each product to show prices, only the first one shows. I have a JsFiddle with a small example of the code i have to show prices. http://jsfiddle.net/c6pso94f/. Should i trying an include-once per while loop and run script as booking.js?

I've tried to use the following:

echo "<span id='$priceblock'> </span>";

var priceblock1 = <?php echo $priceblock; ?>;

$('#'+ priceblock1).text('R ' + total +'/day');

in the code below:

I also have 2 select inputs and 4 checkboxes per item I created variable along the lines of $mySelect = "mySelect"."i"; so that the value of changes with each increased value of $i but that doesn't work as the value is the same for both select inputs. so i made the other one simply $mySelect1 = "mySelect1"."$i";. I have left the full script on Pastebin : https://pastebin.com/HQX3jJKd

  echo "<span>100kms:</span>";
  echo "<span id='$priceblock1'> </span>";
  echo "<span>200kms:</span>";
  echo "<span id='$priceblock2'> </span>";
  echo "<input type='hidden' name='stocknr' value='$stock'>";
  echo "<input type='hidden' name='pricef1' value=''>";
  echo "<input type='hidden' name='pricef2' value=''>"; 

  // Script that adds values together
  <script>
  $(document).ready(function(){
  $('input[name="ch3"]').click(function() {
    $('input[name="ch3"]').not(this).prop('checked', false);
   });
  });

  function displayVals() {
  calcUsage();
  var singleValues = $("#mySelect").val();         
  $("#pricef1").val(singleValues);
  }
  var $cbs = $('input[id="qr1"]');
  function calcUsage() {
  var total = $("#mySelect").val();
  $cbs.each(function() {
    if (this.checked)
        total = parseInt(total) + parseInt(this.value);
  });
  var priceblock1 = <?php echo $priceblock1; ?>;
  $('#' + priceblock1).text('R ' + total +'/day');
  }
  function displayVals1() {
  calcUsage1();
  var singleValues1 = $("#mySelect1").val();         
  $("#pricef2").val(singleValues1);
  }
  var $cbs1 = $('input[id="qr1"]');
  function calcUsage1() {
  var total1 = $("#mySelect1").val();
   $cbs1.each(function() {
    if (this.checked)
        total1 = parseInt(total1) + parseInt(this.value);
   });
    var priceblock2 = <?php echo $priceblock2; ?>;
     $('#'+priceblock2).text('R ' + total1 +'/day');
    }

   $("#mySelect").change(displayVals);
   displayVals();
   $("#mySelect1").change(displayVals1);
   displayVals1();
  //For  checkboxes

  $cbs.click(calcUsage);
  $cbs1.click(calcUsage1);
  </script>         

I'm trying to have each one show the price as it changes from options, checkboxes but it is only showing the first when i was using id="100kms" and id="200kms" respectively. i changed it so $priceblock is $priceblock1 = $priceblock1 + $i

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog