简体   繁体   English

如何使用jquery在按钮单击上显示表单?

[英]how to show form on button click using jquery?

I want my form to appear when i click on the button sign up 我希望我的表单在我单击按钮时显示

<div id="signup">
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="POST"  id="form1">
        <input name="user" type="text" placeholder="Username" size="30" >
        <input name="pass" type="password" placeholder="Password"  size="30" >
        <input class="btn" type="submit" value="sign up" name="signup">
</div>

this is the jQuery code: 这是jQuery代码:

<script>
$( "#form" ).click(function() {
  $( "#signup" ).show( "Clip", 1000 );
});
</script>

You might want to try something like this...(notice I have added an extra "button" to your HTML and closed the "form" tag 您可能想要尝试这样的事情……(注意,我在HTML中添加了一个额外的“按钮”,并关闭了“ form”标签

CSS... CSS ...

#form1 {
 display : none;
}
button {
 margin-bottom: 10px;
}

HTML... HTML ...

<div id="signup">
<button id="signup">Click here to sign-up!</button>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="POST"  id="form1">
        <input name="user" type="text" placeholder="Username" size="30" >
        <input name="pass" type="password" placeholder="Password"  size="30" >
        <input class="btn" type="submit" value="sign up" name="signup">
    </form>
</div>

then using JQuery... 然后使用JQuery ...

$( document ).ready( function() {
  $( "#signup" ).click( function() {
    $( "#form1" ).toggle( 'slow' );
  });
});

JSFiddle JSFiddle

Hope this helps. 希望这可以帮助。 Good luck! 祝好运!

first of all you haven't closed your form tag, second your targeting an element with a id 'form' and any of the elements you have has such id, now one thing you could do is to give each input element in your form a class(signup_txt for example) and target those elements using css and set the property 'display' to none to hide them like this: 首先,您还没有关闭表单标签,其次,您以id为“ form”的元素作为目标,并且您拥有的任何元素都具有该id,现在您可以做的就是为表单中的每个输入元素分配一个class(例如signup_txt),并使用css定位这些元素,并将属性“ display”设置为none,以将其隐藏,如下所示:

.signup_txt{
   display:none
}

then give your submit button an id (signup_btn for example) and do this: 然后为您的提交按钮提供一个ID(例如,signup_btn)并执行以下操作:

$("#signup_btn").click(function(){
  $(".signup_txt").show(); 
});

and that should do the job here's an demo in jsfiddle: http://jsfiddle.net/zdksn35f/ 这应该可以完成工作,这是jsfiddle中的一个演示: http : //jsfiddle.net/zdksn35f/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM