无法读取以表格形式输入的值

问题描述 投票:0回答:1

我是Java语言的新手。我正在入侵其中一个应用程序需要从表单中获取值并对其进行处理:

  icode=document.forms[0].intcode;
        lamt=document.forms[0].lnamt.value;
        nom =document.forms[0].nomon.value;

并根据需要更新表单中其他字段中的值以上三个值是这样的:

document.forms[0].monpmt.value=Math.round(mamt);
            document.forms[0].totamt.value=totamt;

注意:这些值必须根据以上三个条件自动显示在表单中用户输入的值。

function setTotamt() {
   icode=document.forms[0].intcode;
   lamt=document.forms[0].lnamt.value;
   nom =document.forms[0].nomon.value;
   intrate=icode.options[icode.selectedIndex].myvalue;
   if(lamt >0 && nom>0 && intrate>0) { 
     document.forms[0].monpmt.value=Math.round(mamt);
     document.forms[0].totamt.value=totamt;
   }
}

我正在Linux平台和tomcat上执行此操作。

javascript serverside-javascript
1个回答
0
投票
> icode=document.forms[0].intcode;
> lamt=document.forms[0].lnamt.value;
> nom =document.forms[0].nomon.value;

变量应使用var声明,以使其保持局部状态(假定以上内容旨在存在于函数中)。语法或多或少是正确的,它建议使用HTML结构,例如:

<form ...>
  <input name="intcode" ... >
  <input name="lnamt" ... >
  <input name="nomon" ... >
  ...
  <input name="monpmt" ...>
  <input name="totamt" ...>
</form>

因此,如果窗体中的按钮运行一个函数以获取一些值并更新其他控件的值,则它可能看起来像:

  <input type="button" onclick="updateForm(this);" ...>

更新功能可能看起来像:

function updateForm(el) {
  var form = el.form; // store a reference to the form
  var icode = document.forms[0].intcode; // reference to element named "intcode"

  // Get the value of some controls
  var lamt = document.forms[0].lnamt.value;
  var nom = document.forms[0].nomon.value;

  // Do something with the values
  var mamt = ...;
  ...
  var totamt = ...;

  // Set some values in the form
  // Have a reference to the form so use it
  form.monpmt.value = Math.round(mamt);
  form.totamt.value = totamt;
}

请注意,表单控件值始终是字符串,您应该测试所获得的值以确保它们是您期望的值。如果要更新值而无需用户按下按钮[1],则可以使用blur事件来调用updateForm

您将需要验证输入值并处理错误(在用户填写所有字段之前进行更新,而无效数据是显而易见的字段)。同样,您可能需要在放回数据时格式化数据,例如,将数字格式化为两位小数。

  1. 最好是一个按钮,因为它可以是一个提交按钮,这样,如果禁用了javascript或不起作用,该表单将在服务器上提交并更新,并返回更新后的表单。如果可以使用脚本,则可以取消提交并在客户端上进行处理。如果需要,可以对模糊事件和按钮进行更新。
© www.soinside.com 2019 - 2024. All rights reserved.