是否可以制作类似数组的函数? 这样,以下功能可以变得简单并且易于编辑。
function q1() {
var theForm = document.forms["contact-form"];
var quantity = theForm.elements["q1"];
var howmany = 0;
if (quantity.value != "") {
howmany = parseInt(quantity.value);
}
return howmany;
}
function q2() {
var theForm = document.forms["contact-form"];
var quantity = theForm.elements["q2"];
var howmany = 0;
if (quantity.value != "") {
howmany = parseInt(quantity.value);
}
return howmany;
}
function q3() {
var theForm = document.forms["contact-form"];
var quantity = theForm.elements["q3"];
var howmany = 0;
if (quantity.value != "") {
howmany = parseInt(quantity.value);
}
return howmany;
}
现在我改变了这样 GetQuantity()用于从Qty字段中获取值。例如q_A01 .. GetPrice()用于获取只读值Price字段。例如p_A01 .. calculateTotal()用于计算totalprice并返回字段ID“Total”。
function GetQuantity(e) {
var theForm = document.forms["contact-form"];
var quantity = theForm.elements[e];
var howmany =0;
if(quantity.value!=0) {
howmany = parseInt(quantity.value); }
return howmany;
}
function GetPrice(e) {
var theForm = document.forms["contact-form"];
var price = theForm.elements[e];
var howmany =0;
if(price.value!=0) {
howmany = parseInt(price.value); }
return howmany;
}
function calculateTotal()
{
var cakePrice =
GetPrice(p_A01)*GetQuantity(q_A01)+
GetPrice(p_A02)*GetQuantity(q_A02)+
GetPrice(p_A03)*GetQuantity(q_A03)+
GetPrice(p_F11)*GetQuantity(q_F11);
var Totalordered = document.getElementById ("Total");
Totalordered.value = cakePrice;
}
答案 0 :(得分:1)
不确定为什么没有一个答案暗示明显 - 将公共代码保留在函数体中,并使可变代码成为函数参数:
function q(e) {
var theForm = document.forms["contact-form"];
var quantity = theForm.elements[e];
var howmany = 0;
if (quantity.value != "") {
howmany = parseInt(quantity.value);
}
return howmany;
}
或更短版本,与原始版本几乎相同:
function q(e) {
var quantity = document.forms["contact-form"].elements[e];
return (quantity && quantity.value) || 0;
}
答案 1 :(得分:0)
你所有的功能都做同样的事情。只需将不同的数据传递到一个函数中:
<xs:minInclusivevalue="0"/>
<xs:maxInclusivevalue="20"/>
<xs:pattern value="([1-9]?[0-9])|([1-9]?[0-9].5)"/>
但有趣的是,一个变化(数量)的东西是你的功能实际上并没有做任何事情。
这是任何编程语言中函数的基本前提,也是DRY一般最佳实践原则的基础(不要重复自己)。由于您的所有功能都做同样的事情,只需采取不同的(数量)并将其与功能分开。现在,您没有5个存储和维护功能。
答案 2 :(得分:0)
在这里,您可以为所有输入使用一个函数,只需在其中传递输入ID或名称,并获取该元素的值。
private int id, id100px;
private String rollNumber;
private String mobile_number, email_id, nick_name, hobbies, dob, name;
private Testimony[] testimony;
public People(String name, int id, String mobile_number, String email_id, Testimony ... testimony) {
this.name = name;
this.id = id;
this.mobile_number = mobile_number;
this.email_id = email_id;
this.testimony = testimony;
}
public Testimony[] getTestimony() {
return testimony;
}
public String getEmail_id() {
return email_id;
}
public int getId() {
return id;
}
public String getMobile_number() {
return mobile_number;
}
public String getName() {
return name;
}
}
&#13;
答案 3 :(得分:0)
Hi check the following code as you updated
function GetQuantity(e) {
var theForm = document.forms["contact-form"];
var quantity = theForm.elements[e];
var howmany =0;
if(quantity.value!=0) {
howmany = parseInt(quantity.value); }
return howmany;
}
function GetPrice(e) {
var theForm = document.forms["contact-form"];
var price = theForm.elements[e];
var howmany =0;
if(price.value!=0) {
howmany = parseInt(price.value); }
return howmany;
}
function calculateTotal()
{
var cakePrice =
GetPrice('p_A01')*GetQuantity('q_A01')+
GetPrice('p_A02')*GetQuantity('q_A02')+
GetPrice('p_A03')*GetQuantity('q_A03')+
GetPrice('p_F11')*GetQuantity('q_F11');
var Totalordered = document.getElementById ("Total");
Totalordered.value = cakePrice;
}
calculateTotal();