简体   繁体   English

使用JavaScript的表单中的实时计算不起作用

[英]Real-time calculations in a form using JavaScript doesn't work

I actually want to calculate the number of Persons and Children to show in real time a price. 我实际上想计算要实时显示价格的“人数”和“儿童”数量。 This is the form html code: 这是html代码的形式:

    <form class="form-horizontal" id="registerHere" method='post' action="<?php echo get_template_directory_uri(); ?>/admin/register/mailer.php ">

      <fieldset>

        <div class="row"> 

          <!-- Select Basic -->

          <div class="control-group span1">

            <label class="control-label"><?php _e('Adults', 'trek'); ?></label>

            <div class="controls">

              <select id="selectbasic1" name="adults" class="input-xlarge" >
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
                <option>6</option>
              </select>  

            </div>

          </div>

          <div class="control-group span1">

            <label class="control-label"><?php _e('Children', 'trek'); ?></label>

            <div class="controls">

              <select id="selectbasic2" name="childern" class="input-xlarge" >
                <option>0</option>
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
                <option>6</option>
              </select>

            </div>

          </div>

        </div>
                <div class="control-group">

          <div class="controls" id="totalPrice">



          </div>

        </div>


      </fieldset>

      <button type="submit" id="submitted" onsubmit="return validate();"  class="btn"><?php _e('Reserve it', 'trek'); ?></button></br>

    </form>

And this is my JavaScript and it's not working please help me with an advice if u can 这是我的JavaScript,无法正常工作,如果可以的话,请帮助我

<script type="text/javascript">



function getQuantityAdults()
{
//Assume form with id="theform"
var theForm = document.forms["registerHere"];
//Get a reference to the TextBox
var quantity = theForm.elements["selectbasic1"];
var howmany =0;
//If the textbox is not blank
if(quantity.value!="")
{
    howmanyAdults = parseInt(quantity.value)
    howmanyAdults = howmanyAdults * 29;
}
return howmanyAdults;
}

function getQuantityChildren()
{
//Assume form with id="theform"
var theForm = document.forms["registerHere"];
//Get a reference to the TextBox
var quantity = theForm.elements["selectbasic2"];
var howmany =0;
//If the textbox is not blank
if(quantity.value!="")
{
    howmanyChildren = parseInt(quantity.value)
    howmanyChildren = howmanyChildren * 29;
}
return howmanyChildren;
}

function getTotal()
{

var totalPrice = getQuantityAdults() + getQuantityChildren();

//display the result
document.getElementById('totalPrice').innerHTML =
                                  "Total Price For Cake $"+totalPrice;

}
</script>

The big problem here is you're trying to access the form using a name that doesn't exist. 这里最大的问题是您尝试使用不存在的名称访问表单。

document.forms

this requires the name attribute of the form to be able to access it, so use 这要求表单的name属性能够访问它,因此使用

<form name="formbox">...</form>

with

document.forms["formbox"];

For starters, when you're trying to get your form element, you're looking for document.forms["formbox"]; 对于初学者来说,当您尝试获取表单元素时,您正在寻找document.forms [“ formbox”];

You set the id attribute of the form in the html to 'registerHere'. 您在html中将表单的id属性设置为'registerHere'。 You'll need to change one or the other to match. 您需要更改一个或另一个以匹配。 In the example below, I set the form id to 'formbox'. 在下面的示例中,我将表单ID设置为“ formbox”。

Next, you're looking for selectbasic1 and selectbasic2 in the getQuantityAdults and getQuantityChildren functions, but the selects are both named selectbasic. 接下来,您要在getQuantityAdults和getQuantityChildren函数中查找selectbasic1和selectbasic2,但是选择都被命名为selectbasic。

You'll need them both to be unique to work this way, and you'll need to look at them by their exact id. 您需要它们都是唯一的才能以这种方式工作,并且需要按它们的确切ID对其进行查看。 I've named them 1 and 2, respectively. 我分别将它们命名为1和2。

This is enough to get this working for you. 这足以使它为您工作。 One thing I'd like to mention, though, is with howmanyAdults = parseInt(quantity.value) howmanyAdults, howmanyChildren, and howmany. 不过,我想提到的一件事是howmanyAdults = parseInt(quantity.value)howmanyAdults,howmanyChildren和howmany。 You declare how many in both functions but do not use it in either. 您可以声明两个函数中有多少个,但不要在两个函数中都使用它。 howmanyChildren and Adults you declare without the var keyword before it. 您声明的howmanyChildren和Adults之前没有var关键字。 This will declare the variable in the global scope, which is bad practice unless it is needed. 这将在全局范围内声明变量,除非有必要,否则这是一种不好的做法。 I've changed these to just use the howmany variable declared in each function. 我将它们更改为仅使用每个函数中声明的howmany变量。

One last thing. 最后一件事。 You say you want this to work in real time, but there's nothing to trigger the getTotal() function. 您说要实时运行,但是没有什么可以触发getTotal()函数。 I've added something for that at the top of the script. 我已在脚本顶部为此添加了一些内容。

Here's a working example: 这是一个工作示例:

 document.addEventListener('change', function(){ getTotal(); }); function getQuantityAdults() { //Assume form with id="theform" var theForm = document.forms["formbox"]; //Get a reference to the TextBox var quantity = theForm.elements["selectbasic1"]; var howmany =0; //If the textbox is not blank if(quantity.value!="") { howmany = parseInt(quantity.value) howmany = howmany * 29; } return howmany; } function getQuantityChildren() { //Assume form with id="theform" var theForm = document.forms["formbox"]; //Get a reference to the TextBox var quantity = theForm.elements["selectbasic2"]; var howmany =0; //If the textbox is not blank if(quantity.value!="") { howmany = parseInt(quantity.value) howmany = howmany * 29; } return howmany; } function getTotal() { var totalPrice = getQuantityAdults() + getQuantityChildren(); //display the result document.getElementById('totalPrice').innerHTML = "Total Price For Cake $"+totalPrice; } 
 <form class="form-horizontal" id="formbox" name="formbox" method='post' action="<?php echo get_template_directory_uri(); ?>/admin/register/mailer.php "> <fieldset> <div class="row"> <!-- Select Basic --> <div class="control-group span1"> <label class="control-label"><?php _e('Adults', 'trek'); ?></label> <div class="controls"> <select id="selectbasic1" name="adults" class="input-xlarge" > <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> </select> </div> </div> <div class="control-group span1"> <label class="control-label"><?php _e('Children', 'trek'); ?></label> <div class="controls"> <select id="selectbasic2" name="childern" class="input-xlarge" > <option>0</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> </select> </div> </div> </div> <div class="control-group"> <div class="controls" id="totalPrice"> </div> </div> </fieldset> <button type="submit" id="submitted" onsubmit="return validate();" class="btn"><?php _e('Reserve it', 'trek'); ?></button></br> </form> 

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

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