简体   繁体   English

我可以在 Javascript 计算器中使用单选按钮/复选框代替文本框吗?

[英]Can I use radio buttons/checkboxes in place of textboxes in a Javascript calculator?

I have a Javascript calculator in which users can enter quantities of products/features, and it will multiply the quantity by the set price.我有一个 Javascript 计算器,用户可以在其中输入产品/功能的数量,它将数量乘以设定的价格。 Then, the result shows in a textbox below.然后,结果显示在下面的文本框中。 My 2 questions are:我的两个问题是:

  1. I can use <select> options and specify a value for each one like this:我可以使用<select>选项并为每个选项指定一个值,如下所示:

<select name="SITE_EM_4.99" onChange="CalculateTotal(this.form)"> <option value=""> - Select - </option> <option value="1">Yes</option> <option value="0">No</option> </select>

However, it won't let me do the same with checkboxes/radio buttons.但是,它不会让我对复选框/单选按钮做同样的事情。 How can I do that?我怎样才能做到这一点?

Two : Can I change the script so the total shows as actual text, vs in a text field?:我可以更改脚本,使总数显示为实际文本,而不是文本字段吗? THANKS谢谢

PS The script can be found HERE . PS 脚本可以在这里找到。

You can do it almost identically with radio buttons您可以使用单选按钮几乎相同地做到这一点

<input type="radio" name="group1" value="Milk" onClick="CalculateTotal(this.form)" > Milk<br>
<input type="radio" name="group1" value="Butter" onClick="CalculateTotal(this.form)" > Butter<br>

Second question, very easy to do.第二个问题,很容易做到。

Put an empty div tag with an id where you want to show the results.在要显示结果的位置放置一个带有 id 的空 div 标签。 t

Then just insert the values into the div tag as text using javascript below after finishing calculations.然后在完成计算后使用下面的 javascript 将值作为文本插入到 div 标签中。

var output= document.getElementById("outputDiv")
output.innerText = {value of CalculateTotal()}

EDIT: Requested Example编辑:请求的示例

<script>
   function CalculateTotal(myForm)
   {
      //do your calculations here 
      //...
      //...
      var text = //put whatev here that will be the output
      var output= document.getElementById("outputDiv")
      output.innerText = text;
   }
</script>

<body>
   <input type="radio" name="group1" value="1" onClick="CalculateTotal(this.form)" > Yes<br>
   <input type="radio" name="group1" value="2" onClick="CalculateTotal(this.form)" > No<br>
   <div id="outputDiv"></div>

<input type="checkbox" name="SITE_EM_4.99" value="1" onClick="CalculateTotal(this.form)"> 

instead of having an input for the total just switch to a span (or something else) with an id (let's say "total"), and just replace it with innerHTML而不是总的输入只是切换到一个跨度(或其他东西)与一个id(比如说“total”),然后用innerHTML替换它

<script>
function CalculateTotal(frm) {
...
document.getElementById('total').innerHTML = round_decimals(order_total, 2);
...
}
</script>
<script>
document.write("Welcome to my world!!!");
</script>

This function can be used to print text within the document itself instead of in a text field.此 function 可用于在文档本身而不是在文本字段中打印文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM