第二个javascript无法在我的联系表单上工作

时间:2014-10-23 23:44:07

标签: javascript function contact-form-7

好的,我有几个问题。

  1. 我怎样才能让我的总数达到$以及如何在“总计”旁边移动:(所以它会并排)

  2. 一旦我得到总价,是否有一种方法可以将它除以2以将价格的1/2提供到另一个称为“存款”的字段中,然后再将其添加到另一个名为“余额”的字段中“:

  3. 我无法获取要复制的日期字段

  4. 要注意这是我找到如何添加单选按钮(Here

    的地方

    这是我到目前为止所拥有的。

    这是我的表格:

    Session Date: <input type="text" name="field1" id="f1" /> 
    
     Session Type payment :
    <input type="radio" name="sessiontypepayment" id="sessiontypepayment1" value="100.00" onclick="FnCalculate(this,'extrapeople');"> Mini Session $100.00
    <input type="radio" name="sessiontypepayment" id="sessiontypepayment2" value="250.00" onclick="FnCalculate(this,'extrapeople');"> Full Session $250.00
    
    Extra people  :
    <input type="radio" name="extrapeople" id="extrapeople1" value="25.00" onclick="FnCalculate(this,'sessiontypepayment');"> 1 person $25.00
    <input type="radio" name="extrapeople" id="extrapeople2" value="50.00" onclick="FnCalculate(this,'sessiontypepayment');"> 2 people $50.00
    <input type="radio" name="extrapeople" id="extrapeople3" value="75.00" onclick="FnCalculate(this,'sessiontypepayment');"> 3 people $75.00
    <input type="radio" name="extrapeople" id="extrapeople4" value="100.00" onclick="FnCalculate(this,'sessiontypepayment');"> 4 people $100.00
    
    
    Total Amount: <div id="total"></div>
    
    Deposit Amount:
    
    Balance Amount:
    
    balance due BY: <input type="text" name="field2" id="f2" />
    

    到目前为止,这是我的剧本。

      <script type="text/javascript">
        $(document).ready(function(){
            $("#f1").keyup(function(){
            var f2Text = $('#f2').val() + $(this).val();
            $('#f2').val(f2Text );
            });
        });
    </script>
    
    
    <script>
    function FnCalculate(id,name)
    {
         var total=0;
         if(document.getElementById(name+"1").checked == true)
         {
             total = parseInt(document.getElementById(name+"1").value);
         }
         if(document.getElementById(name+"2").checked == true)
         {
             total = parseInt(document.getElementById(name+"1").value);
         }
         total = total + parseInt(id.value);
         document.getElementById("total").innerHTML = total;
    }
    </script>
    

    我真的非常喜欢这方面的帮助。

2 个答案:

答案 0 :(得分:0)

首先,对于最初要检查的HTML单选按钮,您应将默认值设置为checked='checked'。我也会将默认文本放入<div id='total'>$125.00</div>。您可以将jQuery用于它所做的事情,而不是将事件放入HTML中。 .val()返回一个String。将String转换为整数,将+放在它前面。要转换为浮点数,请使用parseFloat()

$(function(){
$('#f1,[name=sessiontypepayment],[name=extrapeople]').change(function(){      
  $('#total').text('$'+(parseFloat($('[name=sessiontypepayment]:checked').val())+parseFloat($('[name=extrapeople]:checked').val())).toFixed(2));
  $('#f2').val($('#f1').val());
});
});

除以2 /2

作为新手,您应该参与使用外部<script src的做法,以便将其缓存到您的浏览器内存中,以便在您的客户重新访问您的网站时加快加载时间。

答案 1 :(得分:0)

首先,清除输入中的所有onclick="FnCalculate(this,'xx');"位。

然后将此<script>替换为:

<script type="text/javascript">
    $(document).ready(function() {
        $("#f1").keyup(function() {
            $('#f2').val($(this).val());
        });
        var inputs = $('input[name="sessiontypepayment"], input[name="extrapeople"]');
        $(inputs).click(function() {
            var total = 0;
            $(inputs).filter(':checked').each(function() {
                total = total + parseInt($(this).val());
            })
            $('#total').html('$' + total);
            $('#deposit').html('$' + (total / 2));
        });
    });
</script>

不确定你的意思&#34;我怎样才能在Total&#34;旁边向上移动?但我怀疑将<div id="total"></div>更改为<span id="total"></span>可以解决您的问题。要么是,要么将一些css应用于div:

#total {
    display: inline;
}

我添加了存款的计算,您需要在某处向您的页面添加<div id="deposit"></div> ...

你需要jquery。在<head>...</head>

之间添加此内容

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>