开发者

How to add class for form label defined in cakephp and then add jquery event

开发者 https://www.devze.com 2023-04-09 07:31 出处:网络
I have created a form in cakephp <?php echo $form->create(\'User\', array(\'action\' => \'login\'));?>

I have created a form in cakephp

<?php echo $form->create('User', array('action' => 'login'));?>
        <?php
          echo '<div class="loginbox">';
          echo '<table width="200" border="0">';
          echo '<tr>';
          echo '<td>';
          echo $form->input('username', array('label' => __('Username', true)));
          echo '</td>';
          echo '<td>';
          echo $form->input('password', array('label' => __('Password', true))); 
          echo '</td>';             
       开发者_运维百科   echo '<td>';
          echo $form->end('Login');
          echo '</td>';
          echo '</tr>';
          echo '</table>';
          echo '</div>';
        ?>

I want to add a class for label and after that add focus event of jquery so that in the input for username the text username appears and when clicked in the input area the text disappears. How to make it work?


I would just bind the focus and blur events to your inputs. For each input, create a title attribute as your default text when the input is empty:

<script type='text/javascript'>
jQuery(document).ready(function() { 
    $(".forminput").focus(function() {
        if ($(this).val() == $(this)[0].title) {
            $(this).removeClass("active");
            $(this).val("");
        }
    });
    $(".forminput").blur(function() {
        if ($(this).val() == "") {
            $(this).addClass("active");
            $(this).val($(this)[0].title);
        }
    });
    $(".forminput").blur();
});
</script>

<input value="" title="Enter a username" name="screenname" type="text" class="forminput" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号