document.Hypersnap.hsresult22.value2 = result2; } 函数formatMessage2(hsunits2,unit_price2){ 返回hsunits2 + * + currencyPrice2(unit_price2)+" = + 货币+ formatPrice2(hsunits2 * unit_price2); } // AltUnits2(备用hsunits2):添加额外的hsunits2以达到最小值 下一个折扣价格 函数getAltUnits2(hsunits2){ var discount_price2 = getDiscountPrice2(hsunits2); if (hsunits2< max_hsunits2)do {hsunits2 ++} while(discount_price2 == getDiscountPrice2(hsunits2)); 返回hsunits2; } 函数findPrice2(){ var hsunits2 = getNumberOfUnits2(); var unit_price2 = getDiscountPrice2(hsunits2); var alt_hsunits2 = getAltUnits2(hsunits2); var alt_unit_price2 = getDiscountPrice2(alt_hsunits2); var result2; if((hsunits2 * unit_price2 )<(alt_hsunits2 * alt_unit_price2)) result2 = formatMessage2(hsunits2,unit_price2); else result2 = formatMessage2(alt_hsunits2,alt_unit_price2 ); showResult2(re sult2); } 函数formatPrice2(value2){ var result2 = Math.floor(value2)+"。" ;; var cents = 100 *(value-Math.floor(value2))+ 0.5; result2 + = Math.floor(美分/ 10); result2 + = Math.floor(分10%); 返回result2; } 函数filterNonNumeric(field){ var result2 = new String(); var numbers =" 0123456789" ;; var chars = field.value.split(" ;"); //创建数组 for(i = 0; i< chars.length; i ++){ if(numbers.indexOf(chars [i])!= - 1)result2 + = chars [i]; } if(field.value2!= result2)field.value = result2; } //结束 - > < / script> < form name =" Hypersnap" method =" post"> & nbsp; < p>< font face =" Arial" size =" 2">< b>所需用户数:< / b>< / font> < input type = text value =" 1"名称= QUOT; hsunits1" onkeydown =" findPrice()" onKeyUp =" filterNonNumeric(this); findPrice()" onkeypress =" findPrice()" size =" 4"风格= QUOT;字型重量:粗体; text-align:center;"> < input type = text onfocus =" this.blur()"名称= QUOT; hsresult1" size =" 24" style =" border:0; font-weight:bold;"> < br>< font face =" Arial" size =" 2">< b>所需用户数:< / b>< / font> < input type = text value =" 1"名称= QUOT; hsunits2" onkeydown =" findPrice()" onKeyUp =" filterNonNumeric(this); findPrice()" onkeypress =" findPrice()" size =" 4"风格= QUOT;字型重量:粗体; text-align:center;"> < input type = text onfocus =" this.blur()"名称= QUOT; hsresult2" size =" 24" style =" border:0; font-weight:bold;"> & nbsp;< / form> < form name =" form1"> < p align =" left">< font color =" black">< strong>总销售额 & nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& n bsp;& nbsp;& nbsp;& nbsp;& nbs p;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp; & nbsp;& nbsp;& nbsp;< input type =" text"命名= QUOT; hstotal"值= QUOT; 0.00" size =" 16" onchange =" ComputeForm()" style =" font-family:Arial; font-style:normal; font-weight:bolder;字体大小:11磅;白颜色; background-color:black;" language =" JavaScript">< / strong>< / font>< / p> < /表格> < / html> Ed于2003年12月14日在太阳报,2003年12月14日写道格林尼治标准时间15:35:44: 希望有人能帮助我。我真的很感激和帮助。 我有一个表格,价格基于''价格突破''。 1.价格突破字段工作很好,但我不能为我的生活添加多个页面。 2.还需要将两个结果添加到''total''字段。 (见附件代码)。 不是硬编码脚本检查的表单元素,而是将表单元素作为对象传递给函数。例如: < FORM name =" someForm" id =" someForm"> < INPUT name =" someTextBox" onkeypress =" checkKey()"> 函数checkKey(){ var txtBox = document.forms [''someForm'']。 elements [''someTextBox'']; //用文本框做点什么 txtBox.value } 我上面介绍的内容类似于您在下面的代码中使用 访问表单的方式。它可以正常工作,但它只能访问硬b / b 编码的表格和控件。人们可以将其改为: < FORM name =" someForm" id =" someForm"> < INPUT name =" someTextBox" onkeypress =" checkKey(this)"> function checkKey(control){ var txtBox = control; > //用文本框做点什么 txtBox.value } 注意函数调用和如何引用控件。上面的 片段实际上是相同的:变量''txtBox''将 引用到同一个控件someTextBox。当关键字this在 中使用内部事件时,它会创建一个对象引用,表示控件的。然后,您可以像使用完全合格的那样使用该参考。 。这样,你可以在多个控件上使用相同的函数 。 这将阻止你复制相同的脚本,并且 允许你使用尽可能多的价格休息(无论它们是什么),就像你想要的那样。以下是对您的HTML的一些评论。 < HEAD> 您正在使用内部事件,但尚未指定 默认脚本语言。包括: < META http-equiv =" Content-Script-Type" content =" text / javascript"> 此处。此外,HTML文档需要TITLE元素(和DOCTYPE)。 < / HEAD> < BODY onLoad =" findPrice();"> < SCRIPT LANGUAGE =" JavaScript"> 类型属性是/ required / in HTML。使用时,它会使 语言属性(不管怎样已弃用)多余。 < SCRIPT type =" text / javascript"> <! - 开始 不要使用SGML评论隐藏SCRIPT内容 - 没有必要作为 这样做的原因不再有效。 < snip> 函数getNumberOfUnits(){ var hsunits1 = document.Hypersnap.hsunits1.value; return(hsunits1 =="")? 0:hsunits1; } 为了使您的脚本能够在更多浏览器中运行,您应该在访问时使用 ''集合语法''表单元素: var hsunits1 = document.forms [''Hypersnap'']。elements [''hsunits1'']。value; < snip> //结束 - > 此结束评论应删除。 < / script> < snipped duplicate script> < form name =" Hypersnap" method =" post"> & nbsp; < p>< font face =" Arial" size =" 2">< b>所需用户数:< / b>< / font> 为什么要使用样式表,还包括弃用的FONT,B和坏的 布局技巧(不间断的空格)? < snip> < input type =" text"命名= QUOT; hstotal"值= QUOT; 0.00"大小= QUOT; 16" < snipped其他属性> language =" JavaScript"> INPUT元素没有语言属性。 希望有所帮助, 迈克 - Michael Winter M。****** @ blueyonder.co.inva 盖子(用.uk替换.invalid) Hoping someone an assist me urgently. I would truly appreciate and help. I have a form and prices are based on ''price break''. 1.The price break fields work fine, but I cannot for the life of me add morethan one to a page.2. Also need to add the two results to a ''total'' field. (See code attached). Many thanks in advance. Sample code ... <HEAD> </HEAD> <BODY onLoad="findPrice();"> <SCRIPT LANGUAGE="JavaScript"><!-- Beginvar max_hsunits1 = 1000; // quantities in excess of max_hsunits1 all havethe same unit pricevar currency = "$"; // currency sign used in ''formatMessage()''// Edit this function to reflect discount prices!function getDiscountPrice(hsunits1) {// Note: Work way down from max to min amounts!if (hsunits1 >= max_hsunits1) return 18;if (hsunits1 >= 750) return 20;if (hsunits1 >= 500) return 25;if (hsunits1 >= 250) return 28;if (hsunits1 >= 150) return 30;if (hsunits1 >= 75) return 31;if (hsunits1 >= 50) return 40;if (hsunits1 >= 25) return 45;if (hsunits1 >= 10) return 47;if (hsunits1 >= 5) return 50;if (hsunits1 >= 1) return 55;if (hsunits1 <= 0) return 0;}function getNumberOfUnits() {var hsunits1 = document.Hypersnap.hsunits1.value;return (hsunits1 == "") ? 0 : hsunits1;}function showResult(result) {// adjust the following line if result must popup somewhere elsedocument.Hypersnap.hsresult1.value = result;}function formatMessage(hsunits1, unit_price) {return hsunits1 + " * " + currency + formatPrice(unit_price) + " = " +currency + formatPrice(hsunits1 * unit_price);}// AltUnits (alternate hsunits1): add extra hsunits1 to reach minimum fornext discount pricefunction getAltUnits(hsunits1) {var discount_price = getDiscountPrice(hsunits1);if (hsunits1 < max_hsunits1) do { hsunits1++ } while (discount_price ==getDiscountPrice(hsunits1));return hsunits1;}function findPrice() {var hsunits1 = getNumberOfUnits();var unit_price = getDiscountPrice(hsunits1);var alt_hsunits1 = getAltUnits(hsunits1);var alt_unit_price = getDiscountPrice(alt_hsunits1);var result;if ((hsunits1 * unit_price) < (alt_hsunits1 * alt_unit_price))result = formatMessage(hsunits1, unit_price);elseresult = formatMessage(alt_hsunits1, alt_unit_price);showResult(result);}function formatPrice(value) {var result= Math.floor(value) + ".";var cents = 100 * (value-Math.floor(value)) + 0.5;result += Math.floor(cents / 10);result += Math.floor(cents % 10);return result;}function filterNonNumeric(field) {var result = new String();var numbers = "0123456789";var chars = field.value.split(""); // create arrayfor (i = 0; i < chars.length; i++) {if (numbers.indexOf(chars[i]) != -1) result += chars[i];}if (field.value != result) field.value = result;}// End --></script><SCRIPT LANGUAGE="JavaScript"><!-- Beginvar max_hsunits2 = 1000; // quantities in excess of max_hsunits2 all havethe same unit pricevar currency = "$"; // currency sign used in ''formatMessage2()''// Edit this function to reflect your discount prices!function getDiscountPrice2(hsunits2) {// Note: It is important to work your way down from max to min amounts!if (hsunits2 >= max_hsunits2) return 18;if (hsunits2 >= 750) return 20;if (hsunits2 >= 500) return 25;if (hsunits2 >= 250) return 28;if (hsunits2 >= 150) return 30;if (hsunits2 >= 75) return 31;if (hsunits2 >= 50) return 40;if (hsunits2 >= 25) return 45;if (hsunits2 >= 10) return 47;if (hsunits2 >= 5) return 50;if (hsunits2 >= 1) return 55;if (hsunits2 <= 0) return 0;}function getNumberOfUnits2() {var hsunits2 = document.Hypersnap.hsunits2.value2;return (hsunits2 == "") ? 0 : hsunits2;}function showResult2(result2) {// adjust the following line if result2 must popup somewhere elsedocument.Hypersnap.hsresult22.value2 = result2;}function formatMessage2(hsunits2, unit_price2) {return hsunits2 + " * " + currency + formatPrice2(unit_price2) + " = " +currency + formatPrice2(hsunits2 * unit_price2);}// AltUnits2 (alternate hsunits2): add extra hsunits2 to reach minimum fornext discount pricefunction getAltUnits2(hsunits2) {var discount_price2 = getDiscountPrice2(hsunits2);if (hsunits2 < max_hsunits2) do { hsunits2++ } while (discount_price2 ==getDiscountPrice2(hsunits2));return hsunits2;}function findPrice2() {var hsunits2 = getNumberOfUnits2();var unit_price2 = getDiscountPrice2(hsunits2);var alt_hsunits2 = getAltUnits2(hsunits2);var alt_unit_price2 = getDiscountPrice2(alt_hsunits2);var result2;if ((hsunits2 * unit_price2) < (alt_hsunits2 * alt_unit_price2))result2 = formatMessage2(hsunits2, unit_price2);elseresult2 = formatMessage2(alt_hsunits2, alt_unit_price2);showResult2(result2);}function formatPrice2(value2) {var result2= Math.floor(value2) + ".";var cents = 100 * (value-Math.floor(value2)) + 0.5;result2 += Math.floor(cents / 10);result2 += Math.floor(cents % 10);return result2;}function filterNonNumeric(field) {var result2 = new String();var numbers = "0123456789";var chars = field.value.split(""); // create arrayfor (i = 0; i < chars.length; i++) {if (numbers.indexOf(chars[i]) != -1) result2 += chars[i];}if (field.value2 != result2) field.value = result2;}// End --></script><form name="Hypersnap" method="post"> &nbsp;<p><font face="Arial" size="2"><b>Number of Users Required :</b></font><input type=text value="1" name="hsunits1" onkeydown="findPrice()"onKeyUp="filterNonNumeric(this); findPrice()" onkeypress="findPrice()"size="4" style="font-weight:bold; text-align:center;"><input type=text onfocus="this.blur()" name="hsresult1" size="24"style="border:0; font-weight:bold;"><br><font face="Arial" size="2"><b>Number of Users Required :</b></font><input type=text value="1" name="hsunits2" onkeydown="findPrice()"onKeyUp="filterNonNumeric(this); findPrice()" onkeypress="findPrice()"size="4" style="font-weight:bold; text-align:center;"><input type=text onfocus="this.blur()" name="hsresult2" size="24"style="border:0; font-weight:bold;">&nbsp;</form><form name="form1"><p align="left"><fontcolor="black"><strong>Total Sales&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<inputtype="text" name="hstotal" value="0.00" size="16"onchange="ComputeForm()"style="font-family:Arial; font-style:normal;font-weight:bolder; font-size:11pt; color:white; background-color:black;"language="JavaScript"></strong></font></p></form></html> 解决方案 "; // currency sign used in ''formatMessage()''// Edit this function to reflect discount prices!function getDiscountPrice(hsunits1) {// Note: Work way down from max to min amounts!if (hsunits1 >= max_hsunits1) return 18;if (hsunits1 >= 750) return 20;if (hsunits1 >= 500) return 25;if (hsunits1 >= 250) return 28;if (hsunits1 >= 150) return 30;if (hsunits1 >= 75) return 31;if (hsunits1 >= 50) return 40;if (hsunits1 >= 25) return 45;if (hsunits1 >= 10) return 47;if (hsunits1 >= 5) return 50;if (hsunits1 >= 1) return 55;if (hsunits1 <= 0) return 0;}function getNumberOfUnits() {var hsunits1 = document.Hypersnap.hsunits1.value;return (hsunits1 == "") ? 0 : hsunits1;}function showResult(result) {// adjust the following line if result must popup somewhere elsedocument.Hypersnap.hsresult1.value = result;}function formatMessage(hsunits1, unit_price) {return hsunits1 + " * " + currency + formatPrice(unit_price) + " = " +currency + formatPrice(hsunits1 * unit_price);}// AltUnits (alternate hsunits1): add extra hsunits1 to reach minimum fornext discount pricefunction getAltUnits(hsunits1) {var discount_price = getDiscountPrice(hsunits1);if (hsunits1 < max_hsunits1) do { hsunits1++ } while (discount_price ==getDiscountPrice(hsunits1));return hsunits1;}function findPrice() {var hsunits1 = getNumberOfUnits();var unit_price = getDiscountPrice(hsunits1);var alt_hsunits1 = getAltUnits(hsunits1);var alt_unit_price = getDiscountPrice(alt_hsunits1);var result;if ((hsunits1 * unit_price) < (alt_hsunits1 * alt_unit_price))result = formatMessage(hsunits1, unit_price);elseresult = formatMessage(alt_hsunits1, alt_unit_price);showResult(result);}function formatPrice(value) {var result= Math.floor(value) + ".";var cents = 100 * (value-Math.floor(value)) + 0.5;result += Math.floor(cents / 10);result += Math.floor(cents % 10);return result;}function filterNonNumeric(field) {var result = new String();var numbers = "0123456789";var chars = field.value.split(""); // create arrayfor (i = 0; i < chars.length; i++) {if (numbers.indexOf(chars[i]) != -1) result += chars[i];}if (field.value != result) field.value = result;}// End --></script><SCRIPT LANGUAGE="JavaScript"><!-- Beginvar max_hsunits2 = 1000; // quantities in excess of max_hsunits2 all havethe same unit pricevar currency = ""; // currency sign used in ''formatMessage2()''// Edit this function to reflect your discount prices!function getDiscountPrice2(hsunits2) {// Note: It is important to work your way down from max to min amounts!if (hsunits2 >= max_hsunits2) return 18;if (hsunits2 >= 750) return 20;if (hsunits2 >= 500) return 25;if (hsunits2 >= 250) return 28;if (hsunits2 >= 150) return 30;if (hsunits2 >= 75) return 31;if (hsunits2 >= 50) return 40;if (hsunits2 >= 25) return 45;if (hsunits2 >= 10) return 47;if (hsunits2 >= 5) return 50;if (hsunits2 >= 1) return 55;if (hsunits2 <= 0) return 0;}function getNumberOfUnits2() {var hsunits2 = document.Hypersnap.hsunits2.value2;return (hsunits2 == "") ? 0 : hsunits2;}function showResult2(result2) {// adjust the following line if result2 must popup somewhere elsedocument.Hypersnap.hsresult22.value2 = result2;}function formatMessage2(hsunits2, unit_price2) {return hsunits2 + " * " + currency + formatPrice2(unit_price2) + " = " +currency + formatPrice2(hsunits2 * unit_price2);}// AltUnits2 (alternate hsunits2): add extra hsunits2 to reach minimum fornext discount pricefunction getAltUnits2(hsunits2) {var discount_price2 = getDiscountPrice2(hsunits2);if (hsunits2 < max_hsunits2) do { hsunits2++ } while (discount_price2 ==getDiscountPrice2(hsunits2));return hsunits2;}function findPrice2() {var hsunits2 = getNumberOfUnits2();var unit_price2 = getDiscountPrice2(hsunits2);var alt_hsunits2 = getAltUnits2(hsunits2);var alt_unit_price2 = getDiscountPrice2(alt_hsunits2);var result2;if ((hsunits2 * unit_price2) < (alt_hsunits2 * alt_unit_price2))result2 = formatMessage2(hsunits2, unit_price2);elseresult2 = formatMessage2(alt_hsunits2, alt_unit_price2);showResult2(result2);}function formatPrice2(value2) {var result2= Math.floor(value2) + ".";var cents = 100 * (value-Math.floor(value2)) + 0.5;result2 += Math.floor(cents / 10);result2 += Math.floor(cents % 10);return result2;}function filterNonNumeric(field) {var result2 = new String();var numbers = "0123456789";var chars = field.value.split(""); // create arrayfor (i = 0; i < chars.length; i++) {if (numbers.indexOf(chars[i]) != -1) result2 += chars[i];}if (field.value2 != result2) field.value = result2;}// End --></script><form name="Hypersnap" method="post"> &nbsp;<p><font face="Arial" size="2"><b>Number of Users Required :</b></font><input type=text value="1" name="hsunits1" onkeydown="findPrice()"onKeyUp="filterNonNumeric(this); findPrice()" onkeypress="findPrice()"size="4" style="font-weight:bold; text-align:center;"><input type=text onfocus="this.blur()" name="hsresult1" size="24"style="border:0; font-weight:bold;"><br><font face="Arial" size="2"><b>Number of Users Required :</b></font><input type=text value="1" name="hsunits2" onkeydown="findPrice()"onKeyUp="filterNonNumeric(this); findPrice()" onkeypress="findPrice()"size="4" style="font-weight:bold; text-align:center;"><input type=text onfocus="this.blur()" name="hsresult2" size="24"style="border:0; font-weight:bold;">&nbsp;</form><form name="form1"><p align="left"><fontcolor="black"><strong>Total Sales&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<inputtype="text" name="hstotal" value="0.00" size="16"onchange="ComputeForm()"style="font-family:Arial; font-style:normal;font-weight:bolder; font-size:11pt; color:white; background-color:black;"language="JavaScript"></strong></font></p></form></html> Ed wrote on 14 Dec 2003 at Sun, 14 Dec 2003 15:35:44 GMT: Hoping someone an assist me urgently. I would truly appreciate and help. I have a form and prices are based on ''price break''. 1.The price break fields work fine, but I cannot for the life of me add more than one to a page. 2. Also need to add the two results to a ''total'' field. (See code attached).Rather than hard-code the form elements that the script checks, passthe form elements as objects to the functions. For example: <FORM name="someForm" id="someForm"><INPUT name="someTextBox" onkeypress="checkKey()"> function checkKey() {var txtBox = document.forms[''someForm''].elements[''someTextBox'']; // Do something with the text boxtxtBox.value} What I''ve presented above is similar to how you access the form inyour code below. It would work fine, but it can only access the hard-coded forms and controls. One could change it to this: <FORM name="someForm" id="someForm"><INPUT name="someTextBox" onkeypress="checkKey(this)"> function checkKey(control) {var txtBox = control; // Do something with the text boxtxtBox.value} Notice the function call and how the control is referenced. Thesnippet above is effectively the same: the variable ''txtBox'' refersto the same control, someTextBox. When the keyword, this, is used inan intrinsic event, it creates an object reference that representsthe control. You can then use that reference in the same way as youwould a fully-qualified one. This way, you can use the same functionon multiple controls. That will stop you from having to duplicate the same script, andallow you to use as many price breaks (whatever they are) as youlike. What follows are some comments on your HTML. <HEAD>You are using intrinsic events, but you have not specified thedefault scripting language. Include: <META http-equiv="Content-Script-Type" content="text/javascript"> here. Also, HTML documents required a TITLE element (and a DOCTYPE). </HEAD> <BODY onLoad="findPrice();"> <SCRIPT LANGUAGE="JavaScript">The type attribute is /required/ in HTML. When used, it renders thelanguage attribute (which is deprecated, anyway) redundant. <SCRIPT type="text/javascript"> <!-- BeginDon''t use SGML comments to hide SCRIPT contents - there''s no need asthe archaic reasons for doing it are no longer valid. <snip> function getNumberOfUnits() { var hsunits1 = document.Hypersnap.hsunits1.value; return (hsunits1 == "") ? 0 : hsunits1; }To make your script work across more browsers, you should use the''collections syntax'' when accessing form elements: var hsunits1 =document.forms[''Hypersnap''].elements[''hsunits1''].value; <snip> // End -->This closing comment should be removed. </script><snipped duplicate script> <form name="Hypersnap" method="post"> &nbsp; <p><font face="Arial" size="2"><b>Number of Users Required :</b></font>Why use style sheets, but also include deprecated FONT, B, and badlayout techniques (non-breaking spaces)? <snip> <input type="text" name="hstotal" value="0.00" size="16"<snipped other attributes> language="JavaScript"> INPUT elements do not have a language attribute. Hope that helps, Mike --Michael Winter M.******@blueyonder.co.invalid (replace ".invalid" with ".uk") 这篇关于形式问题 - 价格突破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 12:38