[英]How to hide the one div on form submit and show another div?
Below is my code. 下面是我的代码。 On button click I need submit the form and pass the value through post. 在按钮上单击,我需要提交表单并将值通过发布传递。 Then the div #con1
should get hidden and it should show the div #con2
. 然后div #con1
应该被隐藏,并且应该显示div #con2
。 In the div #con2
I need to display the value which I get through post but the problem is on click the page keeps on reloading. 在div #con2
我需要显示通过发布获取的值,但是问题出在单击后,页面会继续重新加载。 downvoters kindly mention your comments 投票者请提及您的评论
<?php include("../view/common/head.php"); ?>
<script>
$(document).ready(function() {
$("#myform").submit(function() {
$("#con1").hide();
$("#con2").show();
});
});
</script>
<div class="container" id="con1">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<form name="myform" class="form-horizontal" id="myform" method="post">
<ul class="devices">
<li>
<div class="dev-inner">
<div class="dei-mid"><p>Computer Tower</p></div>
<div class="dei-rgt">
<input type="text" class="form-control inpt-bx-txtclr-home" name="computername" id="computerid" placeholder="000">
</div>
</div>
</li>
</ul>
<button type="submit" id="grad-btn">Calculate</button>
</div>
</form>
</div>
</div>
<div class="container" id="con2" style="display:none">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<p><?php echo $_POST['computername'];?></p>
</div>
</div>
</div>
In your code div
is hide and show working but page goes reload that's why it causes problem 在您的代码div
中,隐藏和显示工作正常,但是页面重新加载,这就是导致问题的原因
I suggest you to use php code instead of jquery 我建议您使用php代码而不是jquery
Just add condition to show div. 只需添加条件以显示div。
Show con1 if form not submitted else show con2 如果未提交表单,则显示con1,否则显示con2
<?php include("../view/common/head.php"); ?>
<?php if(!isset($_POST['computername'])){ ?> <!-Add condition Here->
<div class="container" id="con1">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<form name="myform" class="form-horizontal" id="myform" method="post">
<ul class="devices">
<li>
<div class="dev-inner">
<div class="dei-mid"><p>Computer Tower</p></div>
<div class="dei-rgt">
<input type="text" class="form-control inpt-bx-txtclr-home" name="computername" id="computerid" placeholder="000">
</div>
</div>
</li>
</ul>
<button type="submit" id="grad-btn">Calculate</button>
</div>
</form>
</div>
</div>
<?php }else{ ?>
<div class="container" id="con2" style="display:none">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<p><?php echo $_POST['computername'];?></p>
</div>
</div>
</div>
<?php } ?>
$("#myform").submit(function(e){
e.preventDefault(); // It will prevent the default action.
$("#con1").hide();
$.ajax({
url: "your url",
type: "GET",
success: function(data){
$("#con2").html(data).show();
}
})
});
or you can follow as suggested by @guradio 或者您可以按照@guradio的建议进行操作
try this; 尝试这个;
<script>
$(document).ready(function() {
$("#myform").submit(function(evt) {
evt.preventDefault();
$("#con1").hide();
$("#con2").show();
});
});
</script>
but it will not post the form with this way, you have to send data via ajax or you should submit the form manually. 但它不会以这种方式发布表单,您必须通过ajax发送数据,或者应该手动提交表单。
Well try this in this i have put the button out of form because whenever i am clicking the button jquery is working well but on page load it goes to the very first stage like first is display:block
and second is hide. 好吧,在这方面尝试一下,我使按钮变形了,因为每当我单击按钮时,jquery都运行良好,但是在页面加载时,它进入了第一个阶段,例如first是display:block
,第二个是hide。 So i put the button out of the form. 因此,我将按钮从表单中删除了。 After that i am getting data through jQuery 之后,我通过jQuery获取数据
<script>
$(document).ready(function() {
$("#con2").hide();
$("#grad-btn").click(function() {
$("#con1").hide();
$("#con2").show();
$("#content").html($("#computerid").val());
});
});
</script>
<div class="container" id="con1">
<div class="row" style="margin-top:150px">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<form name="myform" class="form-horizontal" id="myform" method="post">
<ul class="devices">
<li>
<div class="dev-inner">
<div class="dei-mid"><p>Computer Tower</p></div>
<div class="dei-rgt">
<input type="text" class="form-control inpt-bx-txtclr-home" name="computername" id="computerid" placeholder="000">
</div>
</div>
</li>
</ul>
</div>
</form>
<button id="grad-btn">Calculate</button>
</div>
</div>
<div class="container" id="con2" style="margin-top:150px">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<p id="content"></p>
</div>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.