0

当我在最右边的文本框中键入值时,然后单击“单击计算”,我希望所有文本框的值相加并显示。我想不明白。当我单击单击计算按钮时,它什么也不做。不是东西!

<SCRIPT>

function add()
{
var a = document.getElementById("fscf_field4_8").value;
var b = document.getElementById("fscf_field4_10").value;
var c = document.getElementById("fscf_field4_12").value;
var d = document.getElementById("fscf_field4_14").value;
var e = document.getElementById("fscf_field4_16").value;
document.getElementById("fscf_field4_19").value =a+b+c+d+e;
}
</script>

<table>
<tr>
<td>
<div id="FSContact4" style="width:375px;">
<form action="/payment/#FSContact4" id="fscf_form4" method="post">
<input type="hidden" name="fscf_submitted" value="0">
<input type="hidden" name="fs_postonce_4" value="99193296484a8d52d2b9579199ca2f0c,1384214867">
<input type="hidden" name="si_contact_action" value="send">
<input type="hidden" name="form_id" value="4">

<div id="fscf_required4">
  <span style="text-align:left;">*</span> <span style="text-align:left;">indicates required field</span>
</div>

<input type="hidden" name="mailto_id" value="1">

<div id="fscf_div_clear4_4" style="clear:both;">
  <div id="fscf_div_field4_4" style="clear:left; float:left; width:99%; max-width:550px; margin-right:10px;">
    <div id="fscf_label4_4" style="text-align:left; padding-top:5px;">
      <label style="text-align:left;" for="fscf_field4_4">Company Name<span style="text-align:left;">*</span></label>
    </div>
    <div style="text-align:left;">
      <input style="text-align:left; margin:0;" type="text" id="fscf_field4_4" name="company-name" value="">
    </div>
  </div>
</div>

<div id="fscf_div_clear4_5" style="clear:both;">
  <div id="fscf_div_field4_5" style="clear:left; float:left; width:99%; max-width:550px; margin-right:10px;">
    <div id="fscf_label4_5" style="text-align:left; padding-top:5px;">
      <label style="text-align:left;" for="fscf_field4_5">Person Doing the Transaction<span style="text-align:left;">*</span></label>
    </div>
    <div style="text-align:left;">
      <input style="text-align:left; margin:0;" type="text" id="fscf_field4_5" name="person-doing-the-transaction" value="">
    </div>
  </div>
</div>

<div id="fscf_div_clear4_6" style="clear:both;">
  <div id="fscf_div_field4_6" style="clear:left; float:left; width:99%; max-width:550px; margin-right:10px;">
    <div id="fscf_label4_6" style="text-align:left; padding-top:5px;">
      <label style="text-align:left;" for="fscf_field4_6">email<span style="text-align:left;">*</span></label>
    </div>
    <div style="text-align:left;">
      <input style="text-align:left; margin:0;" type="text" id="fscf_field4_6" name="email01" value="">
    </div>
  </div>
</div>
</td>
</tr>
<tr>
<td>
<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="100%" cellpadding="3" cellspacing="3">
        <tr>
                <td>Invoice Number</td>
                <td>Amount (USD)</td>
        </tr>
        <tr>
                <td><input type="text" id="fscf_field4_7 name="Invoice Number 1"></td>
                <td><input type="text" id="fscf_field4_8 name="Amount 1"></td>
        </tr>
        <tr>
                <td><input type="text" id="fscf_field4_9 name="Invoice Number 2"></td>
                <td><input type="text" id="fscf_field4_10 name="Amount 2"></td>
        </tr>
        <tr>
                <td><input type="text" id="fscf_field4_11 name="Invoice Number 3"></td>
                <td><input type="text" id="fscf_field4_12 name="Amount 3"></td>
        </tr>
        <tr>
                <td><input type="text" id="fscf_field4_13 name="Invoice Number 4"></td>
                <td><input type="text" id="fscf_field4_14 name="Amount 4"></td>
        </tr>
        <tr>
                <td><input type="text" id="fscf_field4_15 name="Invoice Number 5"></td>
                <td><input type="text" id="fscf_field4_16 name="Amount 5"></td>
        </tr>
        <tr>
                <td><input type="button" id="click" value="Click to Calculate" onclick="add();"></td>
                <td><input type="text" id="fscf_field4_19"></td>
        </tr>
</table>
<div id="fscf_submit_div4" style="text-align:left; padding-top:2px;">
                <input type="submit" id="fscf_submit4" style="cursor:pointer; margin:0;" value="Submit">
</div>
</form>
</div>
</tr>
</td>
</table>
4

3 回答 3

0

首先,您忘记关闭所有输入 ID 的引号。尝试这样做。IE

<input type="text" id="fscf_field4_8 name="Amount 1">

需要是:

<input type="text" id="fscf_field4_8" name="Amount 1">

解析字符串的 int 值时还需要添加 parseInt() 。下面我把新的代码片段 确认:刚刚用正确的引号和 intParse() 尝试了你的代码,它可以工作

整数解析的最终代码:

<SCRIPT>

function add()
{
    var a = parseInt(document.getElementById("fscf_field4_8").value);
    var b = parseInt(document.getElementById("fscf_field4_10").value);
    var c = parseInt(document.getElementById("fscf_field4_12").value);
    var d = parseInt(document.getElementById("fscf_field4_14").value);
    var e = parseInt(document.getElementById("fscf_field4_16").value);
    document.getElementById("fscf_field4_19").value =a+b+c+d+e;
}
</script>
于 2013-11-14T04:10:36.893 回答
0

您没有table正确关闭您的 id 值。

错误:<input type="text" id="fscf_field4_7 name="Invoice Number 1">上面 的 id value( ) 没有结束引号fscf_field4_7。如果所有文本框都正确,请尝试关闭 id 值。

将您的替换table为以下内容:

<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="100%" cellpadding="3" cellspacing="3">
        <tr>
                <td>Invoice Number</td>
                <td>Amount (USD)</td>
        </tr>
        <tr>
                <td><input type="text" id="fscf_field4_7" name="Invoice Number 1"/></td>
                <td><input type="text" id="fscf_field4_8" name="Amount 1"></td>
        </tr>
        <tr>
                <td><input type="text" id="fscf_field4_9" name="Invoice Number 2"></td>
                <td><input type="text" id="fscf_field4_10" name="Amount 2"></td>
        </tr>
        <tr>
                <td><input type="text" id="fscf_field4_11" name="Invoice Number 3"></td>
                <td><input type="text" id="fscf_field4_12" name="Amount 3"></td>
        </tr>
        <tr>
                <td><input type="text" id="fscf_field4_13" name="Invoice Number 4"></td>
                <td><input type="text" id="fscf_field4_14" name="Amount 4"></td>
        </tr>
        <tr>
                <td><input type="text" id="fscf_field4_15" name="Invoice Number 5"></td>
                <td><input type="text" id="fscf_field4_16" name="Amount 5"></td>
        </tr>
        <tr>
                <td><input type="button" id="click" value="Click to Calculate" onclick="add()"></td>
                <td><input type="text" id="fscf_field4_19"></td>
        </tr>
</table>
于 2013-11-14T04:11:48.440 回答
0

请检查此工作示例http://jsfiddle.net/6AYFg/

首先正确关闭所有标签和属性

将所有元素的值解析为 int

<td><input type="text" id="fscf_field4_7 name="Invoice Number 1"></td>

function add()
{
var a = document.getElementById("fscf_field4_8").value;
var b = document.getElementById("fscf_field4_10").value;
var c = document.getElementById("fscf_field4_12").value;
var d = document.getElementById("fscf_field4_14").value;
var e = document.getElementById("fscf_field4_16").value;
document.getElementById("fscf_field4_19").value =a+b+c+d+e;
}
于 2013-11-14T04:15:57.223 回答