使用下面的jquery:
<script>
$(document).ready(function(){ $("#myiframe").hide(); });
$(document).ready(function(){ $("#submit").click(function(){ $("#myiframe").delay(1000).show(0); }); });
</script>
即使在 php 脚本中,我也可以成功隐藏“if”中声明的 iframe。 见下文:
<?php
$minAge = 22;
$minAge *= 3600*24*365.25; // $minAge in seconds
if(isset($_POST['submit'])){
$birth_date = strtotime($_POST['dob']);
$now = strtotime("now");
$age = $now - $birth_date; // age is in seconds
?>
<iframe id = "myiframe" style='position: absolute; z-index: 10; height: 100%;'
src = "<?php echo ($age > $minAge ? 'http://URL1.com' : 'http://URL2.com'); ?>"
frameborder = "0"
width = "350px"
</iframe>
但是在单击应显示 iframe 的按钮时,我希望它显示一秒的延迟(如 jquery 中所示)。
在此处查看以表单实现的按钮:
<form method="post" id="form">
<p2>Please select your date of birth below to verify your age.</p2>
<input type="date" name="dob">
<input type="submit" name="submit" value="Verify Age" id="submit" />
</form>
完整代码在这里: http://pastebin.com/EaafieB7
基本上它应该做的是:默认隐藏 iframe,在需要时延迟显示。 到目前为止,它只是隐藏成功。
如果有人能指出我错在哪里,我将不胜感激!
最佳答案
好吧,我明白你现在想做什么,但那样是行不通的。有两种方法可以解决此问题:
- 在 PHP 中进行年龄验证检查,并仅在验证成功时才打印 iframe。这样做的缺点是它会重新加载页面,这是您显然不想要的行为。
- 在 Javascript 中进行年龄验证检查,但您必须通过
e.preventDefault()
拦截表单提交处理程序,使其不会重新加载页面。
如果您确实坚持使用 PHP 进行年龄验证,则第三种选择是使用 AJAX 调用,但这有点过头了。
关于javascript - Jquery 正确隐藏 iframe,但点击时不显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29502138/