在此代码中,如果我单击订单详细信息,则应显示该表单,如果我单击应显示该表单的发货详细信息地址,则应隐藏订单表单。 但是,这两个表单都显示在我的代码中。
<!DOCTYPE html>
<html>
<head>
<script>
function orderdetails() {
document.getElementById('orderdetails').style.display = "block";
}
function shippingdetails() {
document.getElementById('shippingdetails').style.display = "block";
}
function ordersummary() {
document.getElementById('welcomeDiv1').style.display = "block";
}
function payment() {
document.getElementById('welcomeDiv1').style.display = "block";
}
</script>
</head>
<body>
<a href="javascript:void(0);" value="Show Div" onclick="orderdetails()" >Order Details</a>
<a href="javascript:void(0);" value="Show Div1" onclick="shippingdetails()">Shipping Details</a>
<a href="javascript:void(0);" value="Show Div12" onclick="ordersummary()">Order Summary</a>
<a href="javascript:void(0);" value="Show Div12" onclick="payment()">Payment</a>
<div id="orderdetails" style="display:none;" class="answer_list" >
<table width="200" border="0">
<h3><u><b>Order Details</b></u></h3>
<tr>
<th scope="row"><label>Name*</label></th>
<td><input name="name" type="text"></td>
</tr>
<tr>
<th scope="row"><label>Email*</label>;</th>
<td><input name="mail" type="text"></td>
</tr>
<tr>
<th scope="row"><label>Mobile Number*</label></th>
<td><input name="mobile" type="text"></td>
</tr>
<tr>
<th scope="row"><label>Land Line</label></th>
<td><input name="phone" type="text"></td>
</tr>
</table>
</div>
<div id="shippingdetails" style="display:none;" class="answer_list" >
<br/>
<table width="200" border="0">
<h3><u><b>Shipping Details</b></u></h3>
<br/>
<tr>
<th scope="row"><label>Full Name*</label></th>
<td><input name="name" type="text"></td>
</tr>
<tr>
<th scope="row"><label>Address*</label></th>
<td><textarea name="address" cols="" rows=""></textarea></td>
</tr>
<tr>
<th scope="row"><label>Nearest Land Mark*</label>;</th>
<td><input name="mail" type="text"></td>
</tr>
<tr>
<th scope="row"><label>City</label></th>
<td><input name="city" type="text"></td>
</tr>
<tr>
<th scope="row"><label>State</label></th>
<td><form name="form" id="form">
<select name="jumpMenu" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
<option>Tamil Nadu</option>
<option>Kerala</option>
<option>Orissa</option>
<option>Delhi</option>
<option>Andhrs</option>
<option>Karntaka</option>
</select>
</form></td>
</tr>
<tr>
<th scope="row"><label>Pin Code*</label></th>
<td><input name="code" type="text"></td>
</tr><tr>
<th scope="row"><label>Mobile Number*</label></th>
<td><input name="city" type="text"></td>
</tr><tr>
<th scope="row"><label>Land Line</label></th>
<td><input name="city" type="text"></td>
</tr>
</table>
</div>
</body>
</html>
答案 0 :(得分:2)
不要只显示表单,也要隐藏其他表单。
function orderdetails() {
document.getElementById('orderdetails').style.display = "block";
document.getElementById('shippingdetails').style.display = "none";
}
function shippingdetails() {
document.getElementById('shippingdetails').style.display = "block";
document.getElementById('orderdetails').style.display = "none";
}
要阻止用户返回上一个表单,您可以检查他们是否已完成该表单。
在表单中,为所需的项目提供一个类名:
<input name="name" type="text" class="order-required">
<input name="mail" type="text" class="order-required">
<input name="mobile" type="text" class="order-required">
并在“订单明细”链接中输入ID:
<a id="orderdetailslink" href="javascript:void(0);" value="Show Div" onclick="orderdetails()" >Order Details</a>
然后在</body>
之前放下以下内容以查找“订购详情”中的所有必填字段:
<script>
var elems = document.getElementsByTagName('*'), i, orderRequired = new Array();
for (i in elems) {
if((' ' + elems[i].className + ' ').indexOf(' ' + 'order-required' + ' ') > -1) {
orderRequired.push(elems[i]);
}
}
</script>
最后使用以下函数检查是否允许用户返回订购详细信息表单。
function orderdetailscomplete() {
for (var i = 0; i < orderRequired.length; i++) {
if (orderRequired[i].value.length == 0) {
return false;
}
}
return true;
}
function orderdetails() {
if (!orderdetailscomplete()) {
document.getElementById('orderdetails').style.display = "block";
document.getElementById('shippingdetails').style.display = "none";
}
}
function shippingdetails() {
document.getElementById('shippingdetails').style.display = "block";
document.getElementById('orderdetails').style.display = "none";
if (orderdetailscomplete()) {
document.getElementById('orderdetailslink').innerHTML = 'Order Details (completed)';
}
}
请参阅fiddle。
答案 1 :(得分:1)
除了脚本之外,您的HTML格式严重错误。你真的需要阅读HTML。 http://validator.w3.org/check甚至无法继续检查,直到您将该标题移出表格代码
这是我的版本。 检查并修复HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Order form</title>
<script>
var divs = [];
function show(div) {
for (var i=0;i<divs.length;i++) {
document.getElementById(divs[i]).style.display=(div===divs[i])?"block":"none";
}
}
function MM_jumpMenu() {} // placeholder
window.onload=function() {
document.getElementById("jumpMenu").onchange=function() {
MM_jumpMenu('parent',this,0);
}
var links=document.getElementById("navigation").getElementsByTagName("a");
for (var i=0;i<links.length;i++) {
divs.push(links[i].id.replace("link",""));
links[i].onclick=function() {
show(this.id.replace("link",""));
}
}
}
</script>
<style>
ul#navigation {
float: left;
margin: 0;
padding: 0;
width: 100%;
background-color: #ccc;
}
ul#navigation li { display: inline; }
ul#navigation li a {
text-decoration: none;
padding: .25em 1em;
border-bottom: solid 1px #39f;
border-top: solid 1px #39f;
border-right: solid 1px #39f;
}
a:link, a:visited { color: #fff; }
ul#navigation li a:hover {
background-color: #fff;
color:#000;
}
.shipping { width:200px; border:0}
</style>
</head>
<body>
<nav>
<ul id="navigation">
<li><a href="#" value="Show Div" id="orderdetailslink">Order Details</a></li>
<li><a href="#" value="Show Div1" id="shippingdetailslink">Shipping Details</a></li>
<!--li><a href="#" value="Show Div12" id="ordersummarylink">Order Summary</a></li>
<li><a href="#" value="Show Div12" id="paymentlink">Payment</a></li -->
</ul>
</nav>
<div id="orderdetails" style="display:none;" class="answer_list" >
<br/>
<h3><u><b>Order Details</b></u></h3>
<table class="shipping">
<tr>
<th scope="row"><label>Name*</label></th>
<td><input name="name" type="text"></td>
</tr>
<tr>
<th scope="row"><label>Email*</label>
</th>
<td><input name="mail" type="text"></td>
</tr>
<tr>
<th scope="row"><label>Mobile Number*</label></th>
<td><input name="mobile" type="text"></td></tr>
<tr>
<th scope="row"><label>Land Line</label></th>
<td><input name="phone" type="text"></td></tr>
</table>
</div>
<div id="shippingdetails" style="display:none;" class="answer_list" >
<br/>
<h3><u><b>Shipping Details</b></u></h3>
<table class="shipping">
<tr>
<th scope="row"><label>Full Name*</label></th>
<td><input name="name" type="text"></td></tr>
<tr>
<th scope="row"><label>Address*</label></th>
<td><textarea name="address"></textarea></td>
</tr>
<tr>
<th scope="row"><label>Nearest Land Mark*</label></th>
<td><input name="mail" type="text"></td>
</tr>
<tr>
<th scope="row"><label>City</label></th>
<td><input name="city" type="text"></td>
</tr>
<tr>
<th scope="row"><label>State</label></th>
<td>
<form name="form" id="form">
<select name="jumpMenu" id="jumpMenu">
<option>Tamil Nadu</option>
<option>Kerala</option>
<option>Orissa</option>
<option>Delhi</option>
<option>Andhrs</option>
<option>Karntaka</option>
</select>
</form></td>
</tr>
<tr>
<th scope="row"><label>Pin Code*</label></th>
<td><input name="code" type="text"></td>
</tr>
<tr>
<th scope="row"><label>Mobile Number*</label></th>
<td><input name="city" type="text"></td>
</tr>
<tr>
<th scope="row"><label>Land Line</label></th>
<td><input name="city" type="text"></td>
</tr>
</table>
</div>
</body>
</html>
答案 2 :(得分:0)
您需要编写一个隐藏已打开表单的代码。你可以这样做:
function closeAll() {
document.getElementById('orderdetails').style.display = "none";
document.getElementById('shippingdetails').style.display = "none";
document.getElementById('welcomeDiv1').style.display = "none";
}
function orderdetails() {
closeAll();
document.getElementById('orderdetails').style.display = "block";
}
function shippingdetails() {
closeAll();
document.getElementById('shippingdetails').style.display = "block";
}
function ordersummary() {
closeAll();
document.getElementById('welcomeDiv1').style.display = "block";
}
function payment() {
closeAll()
document.getElementById('welcomeDiv1').style.display = "block";
}
请参阅隐藏所有div的closeAll函数。只是为了不寻找打开的div。我正在关闭welcomeDiv1。不确定你是否需要它。
答案 3 :(得分:0)
好吧,如果你想一次显示一个表单,你应该先隐藏它们并只显示第一个表单。
然后在每个功能中你都应该隐藏其他功能。到目前为止,FAngel有更好的解决方案。
两个提示:
祝你好运!