如果随机数不是0到15之间,我想显示一个表格。这是我试过的代码。我使用过iframe,但我想要任何其他方法。在booking.html中有简单的表格。我希望它没有iframe怎么做。
function myFunction(){
var x=document.createElement("IFRAME");
x.setAttribute("src","booking.html");
x.style.height="700px";
x.style.border="none";
x.style.float="center";
document.getElementById("ran").appendChild(x);
}
var z=Math.floor(Math.random()*15);
if(z==0){
document.getElementById("ran").innerHTML=
"sorry no seats available </br> Please select another date to book ticket";
}else{
document.getElementById("ran").innerHTML=
"HURRY UP "+z+" seats available !!!</br></br>";
window.onload=myFunction;
}
答案 0 :(得分:0)
当您使用jQuery标记问题时,您可以从load
方法中受益:
在HTML中定义两个区域:一个用于消息,一个用于预订页面:
<div id="ran">
<div id="msg" style="white-space: pre"></div>
<div id="load"></div>
</div>
white-space: pre
只是为了方便文本中的换行符,而不必使用HTML编码。
在Javascript中:
$(function() {
var z = Math.floor(Math.random()*15);
if (!z) {
// Preferably don't use html(), but text():
$("#msg").text("Sorry, no seats available.\nPlease select another date to book ticket");
} else {
$("#msg").text("HURRY UP, " + z + " seats available !!!");
// Here comes the magic of jQuery which does all the Ajax stuff:
$("#load").load('booking.html');
}
});
但是,如果预订页面属于您的网站,为什么不将这些页面合并为一个?您可以包含它,但可以使用style="display:none"
隐藏它。
<div id="ran">
<div id="msg" style="white-space: pre"></div>
<div id="booking" style="display:none"> <!-- hide initially -->
<!--- just example code. Put your own stuff here --->
<form action="book.php" method="POST">
Date from: <input type="text" id="from"><br>
Date until: <input type="text" id="until"><br>
<!-- etcetera .... -->
</form>
</div>
</div>
然后在Javascript中,只有在有座位时才会显示:
$(function() {
var z = Math.floor(Math.random()*15);
if (!z) {
// Preferably don't use html(), but text():
$("#msg").text("Sorry, no seats available.\nPlease select another date to book ticket");
} else {
$("#msg").text("HURRY UP, " + z + " seats available !!!");
$("#booking").show(); // Only show form when there are seats available.
}
});