【问题标题】:Javascript total Price calculationJavascript 总价计算
【发布时间】:2015-04-10 05:02:37
【问题描述】:

我需要创建一个非常基本的函数来计算用户单击按钮时的成本。

假设有三个客户想要旅行;一旦他们点击按钮并选择人数和地点,他们就会看到该地点的总费用。

例如,如果他们选择第二个目的地,那么三个人将是 450 英镑、500 英镑,然后是 550 英镑,所以总和应该是 1500 英镑。

【问题讨论】:

  • 添加你尝试过的代码。
  • 您似乎在寻找可以在不理解的情况下复制和粘贴的代码。这不是它的工作方式。我们是来帮你写代码的,不是给你写代码的。
  • 我已经使用 Html 创建了这个表,但我没有添加代码,因为它很简单,我正在努力使用 javascript,并且不知道从哪里开始。就像我之前所说的,我对 javascript 完全是新手,但希望我会变得更好。感谢您查看我的问题并传递您宝贵的 cmets。
  • 这是我的网页完整代码,Javascript 由下面的成员 Shrif ahmed 提供,但它不适合我。点击这里查看我的网页。 link

标签: javascript


【解决方案1】:

看看下面的例子

$(document).ready(function(){
$('#flights td').on('click', function(){
     var colIndex = $(this).prevAll().length;
     var rowIndex = $(this).parent('tr').prevAll().length + 1;

    var cost = 0;
    var sign = '';
    for (var i = 0; i < rowIndex; i++)
    {
        var value = $('#flights tbody tr:eq(' + i.toString() + ') td:eq(' + (colIndex-1) + ')').html();
        sign = value.substring(0,1);
        cost += Number(value.substring(1));
    }

    $('#cost').text(sign + cost);

});
});

<table id="flights">
<thead>
    <tr>
        <th>Destination</th>
        <th>First</th>
        <th>Second</th>
        <th>Third</th>
    </tr>
</thead>
<tbody>
    <tr>
        <th>One</th>
        <td>$400</td>
        <td>$450</td>
        <td>$500</td>
    </tr>
    <tr>
        <th>Two</th>
        <td>$450</td>
        <td>$500</td>
        <td>$550</td>
    </tr>
    <tr>
        <th>Three</th>
        <td>$500</td>
        <td>$550</td>
        <td>$600</td>
    </tr>
    <tr>
        <th>Four</th>
        <td>$550</td>
        <td>$600</td>
        <td>$650</td>
    </tr>
    <tr>
        <th>Five</th>
        <td>$600</td>
        <td>$650</td>
        <td>$700</td>
    </tr>
</tbody>
</table>

<label>Cost: </label> <label id="cost"></label>

解释:
1- $('#flights td') - 这是一个 jquery 选择器,用于获取名为“#flights”的表中的所有 td(s) ---- 你可以找到更多关于 jquery 选择器Here

2- .on('click', function(){}) - 这里我将一个事件处理函数附加到一个特定事件,即点击 ---- 你可以找到更多关于 .on() @ 987654322@

3- var colIndex = $(this).prevAll().length; - 这里我得到 CLICKED td 的列索引 ---- 你可以找到更多关于 .prevAll() Here

4- var rowIndex = $(this).parent('tr').prevAll().length + 1; - 这里我得到了 CLICKED td 的行索引----你可以找到更多关于 .parent() Here

5- for (var i = 0; i Here

6- var value = $('#flights tbody tr:eq(' + i.toString() + ') td:eq(' + (colIndex-1) + ')').html(); - 在这里,我根据被点击的 td 的静态列索引和循环内的行索引(即 i)来获取 td 内的值。 ---- 你可以找到更多关于 .html() Here

7- value.substring(0,1); - 我刚刚得到的标志可能会有所不同($、€) 这样我就可以在成本标签中连接它。 ---- 你可以找到更多关于 .substring() Here

8- 成本 += 数字(value.substring(1)); - 在这里,我获取每个 td 的值并剥离符号并将其转换为数字并将其添加到将填充在成本标签中的总成本中。

9- $('#cost').text(sign + cost); - 这里只是在成本标签中填充总成本并将符号连接到它。

您可以通过Here了解更多关于jquery的信息,这是一个很好的学习起点。

【讨论】:

  • 我已经尝试过,但计算不起作用。点击这里查看我的网页。 link
  • 你必须引用jquery库... cdn url code.jquery.com/jquery-2.1.3.min.js
  • 这里是一个 jsfiddle 链接 ...jsfiddle.net/cq7aex00/1 ......... 检查左上角的 Frameworks & Extensions 以了解使用的扩展
  • 是的,我已经做到了,而且它工作正常,但是我如何在同一个网页中实现 jquery。我已经托管了我的网站,它不在服务器上,它在我的电脑中,并且计算不起作用。我不知道如何将 jquery 添加到我的 html 文档中。我迷路了。
  • 在您的 html 的头部...添加此行 /跨度>
【解决方案2】:

也许这样的事情会有所帮助: 您可以看到正在运行的示例...

function calc(){
  var tmpText = "";
  var total = 0;
  $( ".price" ).each(function( index ) {
    //strip string into num
    tmpText = $( this ).text();
    tmpText = tmpText.substr(1);;
    //add number
    total+=Number(tmpText)
    console.log( index + ": " + $( this ).text() );
  });
  $("#result").text("$"+total);
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td class='price'>$50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td class='price'>$94</td>
  </tr>
</table>
<button onclick="calc()">calculate</button>
<p id="result"></p>

请确保您将 jquery 添加到您的小提琴中

【讨论】:

  • 我已经尝试过,但它似乎没有工作,但是我正在尝试不同的东西,我从上面的成员那里得到了 javascript,但是它没有在我的网页上进行计算,我不知道为什么。在JSFIDDLE 上查看我的网站
  • 好的,我试过小提琴,你唯一需要做的就是在左上角添加jquery load
  • 非常感谢您的回答,它实际上在小提琴中有效,但在我的网页中却没有,这就是我从 html src="/js/destinations.js"> script> 脚本是相同的,但它不起作用我还需要添加什么吗?
  • 是的,为了将 jQuery 包含到您的网页中,您可以:将代码下载到您的主机并从那里加载 b.使用 直接从 jquery 加载它,您可以在这里选择所有选项:jquery.com/download
  • 我认为您的示例基于 Jquery 而不是 javascript,因为我刚刚意识到,很抱歉,但我需要一个基本的原始 javascript,这就是要求,无论如何感谢您的帮助。
【解决方案3】:

好的,这是一个无 jQuery 的解决方案: (你可以在下面运行它)

function calc() {
  var elems = document.getElementsByClassName("someClass");
  var sum = 0;
  for (var i = 0; i < elems.length; i++) {
    var elem = elems[i];
    var num = elem.innerHTML;
    num = num.substr(1);
    sum += Number(num);
    elem.style.background = "red"
  }
  document.getElementById("cost").innerHTML = "£" + sum;
}
html {
  background: url(../images/background.jpg) no-repeat center center fixed;
  background-size: cover;
  /* Applying image to whole html document */
}
body {
  margin: 10px auto;
}
p {
  font-family: "times new roman";
  font-size: 20px;
  padding-left: 40px;
  padding-right: 30px;
}
a:hover {
  color: red;
}
a:link {
  text-decoration: none;
  /* These are link styles */
}
h1 {
  color: blue;
  text-align: center;
  font-family: courier;
  font-size: 20px;
  background-color: #C0C0C0;
}
table th,
table td {
  padding: 10px;
  border: 1px solid black;
}
td {
  text-align: center;
  cursor: pointer;
}
h2 {
  color: blue;
  font-family: courier;
  font-size: 20px;
  text-align: left;
  padding-left: 40px;
  padding-right: 25px;
}
article {
  font-family: "times new roman";
  background-color: #FFFFFF;
  font-size: 20px;
  margin: 10px;
  padding-left: 15px;
  padding-right: 15px;
  padding-bottom: 10px;
  margin-left: 22%;
  margin-right: 20%;
  /* This is article section */
}
img {
  display: block;
  margin: 0 auto;
  margin-bottom: 5px;
  /* This is image section */
}
#footer {
  width: 640px;
  margin: auto;
  clear: both;
  color: white;
  text-align: center;
  /* This is footer section */
}
/* Below is navigation bar section */

#cssmenu li #cssmenu a {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 14px;
  font-family: Helvetica;
  line-height: 1;
}
#cssmenu ul {
  background: #0033CC url(../images/pattern.png) top left repeat;
  list-style: none;
  margin-left: 26%;
  margin-right: 24%;
}
#cssmenu ul:before {
  content: '';
  display: block;
}
#cssmenu ul:after {
  content: '';
  display: table;
  clear: both;
}
#cssmenu a:link,
#cssmenu a:visited {
  padding: 20px 25px;
  display: block;
  text-decoration: none;
  color: #ffffff;
  text-shadow: 0 -1px 1px #586835;
  border-right: 1px solid #839b4e;
}
#cssmenu a:hover {
  color: #6600FF;
  text-shadow: 0 1px 1px #bdcd9c;
}
#cssmenu li {
  float: left;
}
<body>
  <a id="TOP" name="GoTop"></a>
  <div id="cssmenu">
    <ul>
      <li><a href="index.html">Home</a>
      </li>
      <li><a href="#">Detinations</a>
      </li>
    </ul>
  </div>
  <article>
    <h1> Three Different Cruise Tours </h1>
    <p>We are currently offering three different Cruise tours....</p>

    <h2> Paris & French Countryside Cruise tour </h2>
    <img src="images/french.jpg" alt="Paris and French" style="width:90%;height:400px">
    <p>You'll be dazzled by the city of romance with visits to the legendary Notre Dame Cathedral and the opulent Palace of Versailles....</p>
    <h2> Swiss Splendors Cruise tour </h2>
    <img src="images/swiss.jpg" alt="swiss splendors" style="width:90%;height:400px">
    <p>From the magnificent snow caps of the Bernese and Swiss Alps to the enchanting gardens surrounding Lake Como....</p>
    <h2>Europe's Imperial Treasures Cruise tour</h2>
    <img src="images/russia.jpg" alt="European" style="width:90%;height:400px">
    <p>You'll be immersed in the unique cultures of Budapest, Vienna and Prague ...</p>
    <h1> Price Guide </h1>
    <p>click on field to calculate the price.
      <p>

        <table id="flights">
          <thead>
            <tr>
              <th>Destination</th>
              <th>First</th>
              <th>Second</th>
              <th>Third</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th>One</th>
              <td class="someClass">£400</td>
              <td>£450</td>
              <td>£500</td>
            </tr>
            <tr>
              <th>Two</th>
              <td class="someClass">£450</td>
              <td>£500</td>
              <td>£550</td>
            </tr>
            <tr>
              <th>Three</th>
              <td class="someClass">£500</td>
              <td>£550</td>
              <td>£600</td>
            </tr>
            <tr>
              <th>Four</th>
              <td class="someClass">£550</td>
              <td>£600</td>
              <td>£650</td>
            </tr>
            <tr>
              <th>Five</th>
              <td class="someClass">£600</td>
              <td>£650</td>
              <td>£700</td>
            </tr>
          </tbody>
        </table>

        <button onclick="calc()">calculate first col</button>
        <label>Cost:</label>
        <label id="cost"></label>

  </article>
  <div id="footer">
    <br>
    <p>&copy; 2014</p>
  </div>
  <a href="#TOP">Go To Top</a>	
</body>

【讨论】:

  • 非常感谢您的回答,但是 sherif ahmed 已经完成了,我可以使用他的示例,但是我需要 javascript 而不是 Jquery,但感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
相关资源
最近更新 更多