javaScript将两个数字相乘并将结果显示为第三个html输入

时间:2015-05-08 08:45:54

标签: javascript jquery html

我正在尝试从我的html表单中获取一个数字,然后将其与我的JavaScript函数中的数字相乘,并将结果显示在另一个html输入中。我想要的是当用户点击结果输入区域时,只显示乘法结果。

<script type="text/javascript">
  function myFunction() {
    var y = document.getElementById("km").value;
    var z = 14;
    var x = y * z;
    document.getElementById("bill").innerHTML = x;


  }
<table>
<tr> 
   <td>Total Kms Run</td>
   <td><input type="text" id="km" name="km" required>
   </tr>
    <tr> 
   <td>Total Bill</td>
   <td><input type="text" id = "bill" name="bill" required onclick="myFunction()">
   </tr>
</table

3 个答案:

答案 0 :(得分:2)

您正在添加数字,而不是将它们相乘:)。

除此之外,input没有innerHTML,您需要设置value

document.getElementById("bill").value = x;

答案 1 :(得分:1)

像这样改变

protected void ASPxButton1_Click(object sender, EventArgs e)
{
    MailMessage mail = new MailMessage();

    mail.From = new MailAddress("paragonadmin@qss.co.za", "Paragon Admin");
    mail.To.Add(new MailAddress("dewald@qss.co.za", "Appointment Scheduled"));
    mail.Subject = "Enter mail subject";
    mail.Body = "Enter mail body";

    SmtpClient smtpClient = new SmtpClient("smtp.qss.co.za");
    smtpClient.Credentials = new System.Net.NetworkCredential("paragonadmin@qss.co.za", "123456");
    Object state = mail;

    //event handler for asynchronous call
    smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
    try
    {
        //smtpClient.Send(mail);
        smtpClient.SendAsync(mail, state);
    }
    catch (Exception ex) { /* exception handling code here */ }
}

void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    MailMessage mail = e.UserState as MailMessage;

    if (!e.Cancelled && e.Error != null)
    {
        ASPxLabel1.Text = "Mail sent successfully";
    }
}

答案 2 :(得分:1)

你在html中犯了很多语法错误

&#13;
&#13;
function myFunction() {
    var y = document.getElementById("km").value;
    var z = 14;
    var x = Number(y) * Number(z);
    document.getElementById("bill").value = x;

}
&#13;
<table>
   <tr> 
       <td>Total Kms Run</td>
       <td><input type="text" id="km" name="km" required ></td>
  </tr>
  <tr> 
       <td>Total Bill</td>
      <td><input type="text" id="bill" name="bill" required onclick="myFunction()"></td>
   </tr>
</table>
&#13;
&#13;
&#13;