求助用JAVASCRIPT做一个简单选择题。就像图里这样的,我已经列好了FORM,需要做等式。感谢!~

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求助用JAVASCRIPT做一个简单选择题。就像图里这样的,我已经列好了FORM,需要做等式。感谢!~相关的知识,希望对你有一定的参考价值。

这是一个关于假期花费的选择的计算题,
上面是旅游类型TOUR TYPE,分别花费650, 850,1150,1450.
下面是不同的折扣,折扣率是15%,10%,5%,0%。
checkbox里是是否坐飞机,另加400
最后calculate是算出总和。同时要弹出警告如果没有选上TOUR TYPE或者PAYMENT DATE的话。

试试这样应该是可以的,就是你的折扣率那我没太弄清楚你要的算法.算法那你看着根据需求改改.
总价格目前是totalPrice = price * (1 - discount)+400.(不明白你为什么1+?或者不用1直接乘?)

<script type="text/javascript" language="javascript">
// Calculate price, discount and display the total cost of selected
// Tour
// Payment
// Airfare
function calculateTotal()
priceString = "", discountString = "";
for (i = 0; i < document.all.tour.length; i++)
if (document.all.tour[i].checked)
priceString = document.all.tour[i].value;


for (i = 0; i < document.all.date.length; i++)
if (document.all.date[i].checked)
discountString = document.all.date[i].value;


if (priceString == "")
alert("Please select a tour.");

else if (discountString == "")
alert("Please select a departure date period.");
else
price = parseInt(priceString);
discount = parseFloat(discountString);
totalPrice = price * (1 - discount);
if (document.all.Air.checked == true)
totalPrice += parseFloat(document.all.Air.value);

alert("The approximate cost of the holiday is $" + totalPrice.toFixed(2));


</script>
</head>
<body>
<h1>Fiji Holiday Price Calculator</h1>
<form>
<p>Complete the form below to calculate the cost of your Fiji Holiday.</p>
<p>Tour type:</p>
<p><input type="radio" id="tour" name="tour" value="650" />
<label for="Tour1">5-day Fiji Escape Tour</label><br />
<input type="radio" id="tour" name="tour" value="850" />
<label for="Tour2">7-day Fiji Panorama Tour</label><br />
<input type="radio" id="tour" name="tour" value="1150" />
<label for="Tour3">10-day Fiji Paradise Tour</label><br />
<input type="radio" id="tour" name="tour" value="1450" />
<label for="Tour4">14-day Fiji Paradise Tour</label>
</p>
<p>Payment date:</p>
<p><input type="radio" id="date" name="date" value="0.15" />
<label for="Payment1">Before 1st March 2011</label><br />
<input type="radio" id="date" name="date" value="0.10" />
<label for="Payment2">Between 1st and 31st March 2011</label><br />
<input type="radio" id="date" name="date" value="0.05" />
<label for="Payment3">Between 1st and 30th April 2011</label><br />
<input type="radio" id="date" name="date" value="0" />
<label for="Payment4">After 30th April 2011</label>
</p>
<p><label for="Airfare">Click here if you want to include an airfare from Brisbane, Sydney or Melbourne:
<input type="checkbox" id="Airfare" name="Air" value="400" /></label>
</p>
<p><input type="button" value="Calculate" onclick="calculateTotal();"/> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
参考技术A <script type="text/javascript" language="javascript">
// Calculate price, discount and display the total cost of selected
// Tour
// Payment
// Airfare
function calculateTotal()
priceString = "", discountString = "";
for (i = 0; i < document.form.tour.length; i++)
if (document.form.tour[i].checked)
priceString = document.form.tour[i].value;


for (i = 0; i < document.form.date.length; i++)
if (document.form.date[i].checked)
discountString = document.form.date[i].value;


if (priceString == "")
alert("Please select a tour.");

else if (discountString == "")
alert("Please select a departure date period.");
else
price = parseInt(priceString);
discount = parseFloat(discountString);
totalPrice = price * (1 + discount);
if (document.form.Air.checked == true)
totalPrice += parseFloat(document.form.Air.value);

alert("The approximate cost of the holiday is $" + totalPrice.toFixed(2));


</script>
参考技术B <script type="text/javascript" language="javascript">
// Calculate price, discount and display the total cost of selected
// Tour
// Payment
// Airfare
function calculateTotal (t,p,a)
var t = parseInt (document.getElementById ("Tour").value+i, 10);
var p = parseInt (document.getElementById ("Payment").value+i, 10);
var a = parseInt (document.getElementById ("Airfare").value, 10);
if (radio.checked == true)
alert ("You must select both tour type and payment date");
document.getElementById("Tour").value = "";
return false;


var price = t;
var saving = price * discount;
var subtotal = price - saving;
var total = subtotal + a;
return total;
alert ("The approximate cost of the holiday is $" + total);

</script>
</head>
<body>
<h1>Fiji Holiday Price Calculator</h1>
<form>
<p>Complete the form below to calculate the cost of your Fiji Holiday.</p>
<p>Tour type:</p>
<p><input type="radio" id="Tour1" name="tour" value="650" />
<label for="Tour1">5-day Fiji Escape Tour</label><br />
<input type="radio" id="Tour2" name="tour" value="850" />
<label for="Tour">7-day Fiji Panorama Tour</label><br />
<input type="radio" id="Tour3" name="tour" value="1150" />
<label for="Tour3">10-day Fiji Paradise Tour</label><br />
<input type="radio" id="Tour4" name="tour" value="1450" />
<label for="Tour4">14-day Fiji Paradise Tour</label>
</p>
<p>Payment date:</p>
<p><input type="radio" id="Payment1" name="date" value="0.15" />
<label for="Payment1">Before 1st March 2011</label><br />
<input type="radio" id="Payment2" name="date" value="0.10" />
<label for="Payment2">Between 1st and 31st March 2011</label><br />
<input type="radio" id="Payment3" name="date" value="0.05" />
<label for="Payment3">Between 1st and 30th April 2011</label><br />
<input type="radio" id="Payment4" name="date" value="0" />
<label for="Payment4">After 30th April 2011</label>
</p>
<p><label for="Airfare">Click here if you want to include an airfare from Brisbane, Sydney or Melbourne:
<input type="checkbox" id="Airfare" name="Air" value="400" /></label>
</p>
<p><input type="button" value="Calculate" onclick="calculateTotal(t,p,a);"/> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>

这是我目前的代码,SCRIPT是有问题的,算不出来。请大家根据我的FORM帮我看看我的SCRIPT
参考技术C 结合楼上我修改了一下我的代码如下,但是还是有错误,浏览器提示网页错误详细信息

消息: 'document.form.tour' 为空或不是对象
行: 14
字符: 14
代码: 0

<script type="text/javascript" language="javascript">
// Calculate price, discount and display the total cost of selected
// Tour
// Payment
// Airfare
function calculateTotal()
priceString = "", discountString = "";
for (i = 0; i < document.form.tour.length; i++)
if (document.form.tour[i].checked)
priceString = document.form.tour[i].value;


for (i = 0; i < document.form.date.length; i++)
if (document.form.date[i].checked)
discountString = document.form.date[i].value;


if (priceString == "")
alert("Please select a tour.");

else if (discountString == "")
alert("Please select a departure date period.");
else
price = parseInt(priceString);
discount = parseFloat(discountString);
totalPrice = price * (1 + discount);
if (document.form.Air.checked == true)
totalPrice += parseFloat(document.form.Air.value);

alert("The approximate cost of the holiday is $" + totalPrice.toFixed(2));


</script>
</head>
<body>
<h1>Fiji Holiday Price Calculator</h1>
<form>
<p>Complete the form below to calculate the cost of your Fiji Holiday.</p>
<p>Tour type:</p>
<p><input type="radio" id="tour1" name="tour" value="650" />
<label for="Tour1">5-day Fiji Escape Tour</label><br />
<input type="radio" id="tour2" name="tour" value="850" />
<label for="Tour2">7-day Fiji Panorama Tour</label><br />
<input type="radio" id="tour3" name="tour" value="1150" />
<label for="Tour3">10-day Fiji Paradise Tour</label><br />
<input type="radio" id="tour4" name="tour" value="1450" />
<label for="Tour4">14-day Fiji Paradise Tour</label>
</p>
<p>Payment date:</p>
<p><input type="radio" id="date1" name="date" value="0.15" />
<label for="Payment1">Before 1st March 2011</label><br />
<input type="radio" id="date2" name="date" value="0.10" />
<label for="Payment2">Between 1st and 31st March 2011</label><br />
<input type="radio" id="date3" name="date" value="0.05" />
<label for="Payment3">Between 1st and 30th April 2011</label><br />
<input type="radio" id="date4" name="date" value="0" />
<label for="Payment4">After 30th April 2011</label>
</p>
<p><label for="Airfare">Click here if you want to include an airfare from Brisbane, Sydney or Melbourne:
<input type="checkbox" id="Airfare" name="Air" value="400" /></label>
</p>
<p><input type="button" value="Calculate" onclick="calculateTotal();"/> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>

求助如何在javascript中通过iframe url定位iframe

一个网页中有很多iframe ,知道iframe 的url地址,如何定位该frame?

一般都是用iframe id来定位(document.getElementById('iframe的ID')),但是我只能获取到iframe url。

谢谢。

参考技术A 推荐用jquery,用class和id都可以获取元素,如果两样都没有,可以用jquery的each来循环,既然你能知道url,那就在循环中判断是url,就能得到 你想要的元素(iframe)了 参考技术B function getIframeByURL(url)

    var frames = document.frames;

    for(var i = 0; i < frames.length; i++)

        if(frames[i].location==url)

            return frames[i];

        

    

    return null;

本回答被提问者采纳
参考技术C

小菜一碟,亲,请验收哦

追问

非常感谢,有没有不用jquery的方法?用jquery不很方便。

追答

OK ,如亲所愿!

追问

不好意思。刚刚没看到你的回答,正好第一个人的答案是对的,所以就把分给他了。你去链接地址回答下,补分给你。

http://zhidao.baidu.com/question/556257802?quesup2&oldq=1

以上是关于求助用JAVASCRIPT做一个简单选择题。就像图里这样的,我已经列好了FORM,需要做等式。感谢!~的主要内容,如果未能解决你的问题,请参考以下文章

求助:关于用javascript编的一个脚本

jQuery(javascript)无法跨域,怎么解决?是想获取iframe的框架里面的东西!!

在线求助,用ultraiso制作的u盘启动,usb跟uefi,选择哪个启动

求助!怎样向别人的网站提交表单并返回信息?

求助,用python编写一个模拟个人所得税计算器程序,数据看图

求助:用delphi做了个简单浏览器,可以浏览网页,但一打开里面其他的连接,确是用IE打开的。