I want to show a hidden form when click on button my problem is when I click on the button the form is displayed for seconds then the page reload this is my code:
Java Script:
function show(){
document.getElementById("theForm").style.display="block";
}
html :
<button id="search" onclick="show()">show</button>
</form>
<form class="form-horizontal" action="FormController/save" method = "POST" id="theForm" style="display: none">
Default behavior of button
is submit
, hence the form
is submitted. You can use type="button"
which doesn't have default behavior, thus it can be associated with client-side event handlers.
<button type="button" id="search" onclick="show()">show</button>
Additionally, I would recommend you do get rid of of ugly inline click handlers and bind event handler using addEventListener()
document.querySelector('#search').addEventListener("click", show, false);
HTML
<button type="button" id="search">show</button>
If you're using jQuery as your tags says so you could do it like :
$('#seach').on('click', function(){
$('#theForm').show();
})
Hope this helps.
$('#search').on('click', function(){ $('#theForm').show(); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="search">Show</button> <br> <form class="form-horizontal" action="FormController/save" method = "POST" id="theForm" style="display: none"> MY FORM </form>
Try This:
Java Script:
function show(){
document.getElementById("theForm").style.display="block";
}
html :
<input type="button" onclick="show()" id="search" value="show">
</form>
<form class="form-horizontal" action="FormController/save" method = "POST" id="theForm" style="display: none">
<script>
$(document).ready(function(){
$("#what").click(function() { //call event
$(".hello").hide();//hide forms
$('neededFormOnly').show(); //show only the needed form
return false
});
});
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.