简体   繁体   English

创建可点击的按钮并弹出

[英]create clickable button and pop-up

I have 'create user' form and needed litte help. 我有“创建用户”表格,需要帮助。 At one of the link i wanted to user to click on the button and then small pop-up will pop with information then they close the pop-up and continue filling out the form. 在我要用户单击的链接之一上,然后将弹出带有信息的小弹出窗口,然后他们关闭弹出窗口并继续填写表格。 following is the code: 以下是代码:

<div class="users form">
<?php
    echo $this->Form->create('User', array (
            'type' => 'post',
            'inputDefaults' => array (
                'div' => false
            )
        )       
    );
?>
<script>
    $(document).ready(function() {
        $('#UserFirstName').focus();
    });
</script>
<fieldset>
    <legend></legend>
    <h2>Registration</h2>
    <?php
        echo $this->Form->input('firstName');
        echo '<div class=\'clear\'></div>';
        echo $this->Form->input('lastName');
        echo '<div class=\'clear\'></div>';
        echo $this->Form->input('username');
        echo '<div class=\'clear\'></div>';
        echo $this->Form->input('password', array ('class' => 'short'));
        echo '<div class=\'clear\'></div>';
        echo $this->Form->input('password_confirm', array('type' => 'password', 'label' => 'Confirm Password:   ', 'class' => 'short'));
        echo '<div class=\'clear\'></div>';
        echo $this->Form->input('email', array('label' => 'Email:   ', 'default' => $email));
        echo '<div class=\'clear\'></div>';
        echo $this->Form->input('id', array('label' => 'id: ', 'type' => 'hidden', 'default' => 37));
        $qmark = $this->Html->image('qmark.png', array('height' => 15));
        echo '<div class=\'clear\'></div>';
        echo $this->Form->input('number', array('label' => 'Number:', 'class' => 'short', 'after' => $qmark));
        echo '<div class=\'clear\'></div>';
    ?>

I want user to click qmark button and small window will pop-up with definatation of that field. 我希望用户单击qmark按钮,然后会弹出一个小窗口,其中包含该字段的定义。

echo $this->Form->input('number', array('label' => 'Number:', 'class' => 'short', 'after' => $qmark));

Thanks in advance for the help. 先谢谢您的帮助。

Here you are! 这个给你!

    $(document).ready(function() {

       $('input[name="number"]').click(function() {

          alert('Display your popup here...'); // maybe you should use another js library for popup
      });
    });

Byee! 再见!

Indeed you must add a listener of click event on the button by using: 实际上,您必须使用以下命令在按钮上添加click事件的侦听器:

$('input[name="number"]').click(function() {
    // Your code here

}

You could use the famous JQuery-UI dialog box : http://jqueryui.com/demos/dialog/ It is very easy to use. 您可以使用著名的JQuery-UI对话框: http : //jqueryui.com/demos/dialog/它非常易于使用。

You can create the content of the box in the main page (in a div with the display: none style), or previously retrieve the content with an ajax call, and then openning dialog box with the content. 您可以在主页中创建框的内容(在显示为div的div中),或者以前使用ajax调用检索内容,然后打开包含内容的对话框。

You have to bind (add a listener) to the button when a user clicks it so it can show your pop-up, dont forget to add the ID to the button or element you want to make clickable, I suggest using this tag: 您必须在用户单击按钮时将其绑定(添加一个侦听器)到该按钮,以便它可以显示您的弹出窗口,不要忘记将ID添加到您希望使其可单击的按钮或元素,我建议使用此标记:

<a href="#"></a>

$("#button").click(function(e){
    e.preventDefault();
    $("#popup").show();
});

If you use tag you have to add the preventDefault(), to prevent the browser to change the focus of the cursor. 如果使用标记,则必须添加preventDefault(),以防止浏览器更改光标的焦点。

Also you have to create the "pop-up" using CSS to float over the other elements of the site, because another browser window will be probably blocked by default and I consider its a bad usability practice. 另外,您还必须使用CSS创建“弹出式窗口”,以使其悬浮在网站的其他元素上,因为默认情况下可能会阻止另一个浏览器窗口,并且我认为这是一种不好的可用性做法。

Dont forget to hide the pop-up box, you can do that with CSS with the following property: 不要忘记隐藏弹出框,可以使用具有以下属性的CSS来做到这一点:

visible:none;

here i have done complete solution for above issue. 在这里,我已经完成了上述问题的完整解决方案。

Demo http://codebins.com/bin/4ldqp6y 演示 http://codebins.com/bin/4ldqp6y

HTML 的HTML

<input type="button" id="btnSignUp" value="SignUp" />
<div id="popup">
  <div id="popupinfo">
    <p> Now, your registration form has been loaded...! </p>
    <p> You have field your required fields and submit form.</p>
    <p class="button"><a href="javascript:void(0);">OK</a></p>
  </div>
</div>
<div id="registerDiv">
  <p class="required">Fields with Mark (*) are required fields </p>
  <form name="frmregister" id="frmregister" method="post" action="#registerDiv">
    <table cellpadding="0" cellspacing="0" class="table" border="0">
      <tr>
        <td>*Name:</td>
        <td><input type="text" class="input" size="17" /></td>
      </tr>
      <tr>
        <td>*Username:</td>
        <td><input type="text" class="input" size="17" /></td>
      </tr>
      <tr>
        <td>*Password:</td>
        <td><input type="password" class="input" size="17" /></td>
      </tr>
      <tr>
        <td>*Confirm Password:</td>
        <td><input type="password" class="input" size="17" /></td>
      </tr>
      <tr>
        <td>Email:</td>
        <td><input type="text" class="input" size="17" /></td>
      </tr>
      <tr>
        <td colspan="2">&nbsp;</td>
      </tr> 
       <tr>
         <td colspan="2" align="center"><input type="submit" value="Register" id="btnsubmit" /></td>
       </tr>
    </table>
  </form>
</div>

CSS 的CSS

.input{
  border:1px solid #333;
}
.required{
  color:red;
  font-size:12px;
}

#registerDiv{
  display:none;
  margin-top:10px;
  margin-left:20px;
  border:2px solid #333;
  padding:3px;
  background:#cdcdcd;
  width:280px;
  text-align:center;
}
#popup{
  display:none;
  padding:10px;
  background:#969696;
  position:absolute;
  top:25%;
  left:25%;
  z-index: 99999;
  opacity:100%;

}
#popupinfo{
  padding:2px;
  text-align:center;
  background:#cfcfcf;
}
#popupinfo p{
  font-size:14px;
}

#popupinfo .button {
  text-align:center;
}
#popupinfo .button a{
  text-decoration:none;
  border:1px solid #333;
  width:20px;
  padding:5px;
  background:#1A1A1A;
  color:#eee;
}

#popupinfo .button a:hover{
  background:#eee;
  color:#1a1a1a;
}

#mask{

  background: #000;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 10;
  width: 100%;
  height: 100%;
  opacity: 0.8;
  z-index: 999;

}

jQuery jQuery的

$(function() {
    $("#btnSignUp").click(function() {
        $("#registerDiv").fadeIn(300);
        $("body").append("<div id='mask'></div>");
        $("#mask").fadeIn(300);
        $("#popup").fadeIn(300);
    });
    $("#popup .button a").click(function() {
        $("#popup").fadeOut(300, function() {
            $("#mask").remove();
        });
        $("#frmregister").find("input:first").focus();
    });
});

Demo http://codebins.com/bin/4ldqp6y 演示 http://codebins.com/bin/4ldqp6y

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

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