0

当用户在文本框中键入他的用户名时,我正在尝试创建一个欢迎页面。当他单击提交按钮时,我希望在进入下一页之前使用 jquery 提醒他的名字。这是我在可能的代码中得到的:

<html>
    <head>
    <?php   echo $this->Html->script("jquery-1.4.3.min"); ?>

    </head>
    <body>
    <?php
        echo $content_for_layout;
    ?>

    <script type="text/javascript">
        $(document).ready(function () {
         //       alert('JQuery is succesfully included');

        $(':submit').click(function(){
            <?php if(($this->request->controller=='members')&&($this->action=='register')):?>
                    var some = $("#myId").val();
                    alert(some);
            <?php  endif; ?>
            });

        });


    </script>
    </body>
</html>

但它在警报框中不显示任何内容。

我也试过这个:

<html>
    <head>
        <?php   echo $this->Html->script("jquery-1.4.3.min"); ?>

    </head>
    <body>
    <?php
        echo $content_for_layout;
    ?>

    <script type="text/javascript">
        $(document).ready(function () {
         //       alert('JQuery is succesfully included');

        $(':submit').click(function(){
            <?php if(($this->request->controller=='members')&&($this->action=='register')):?>
                    //alert('well');
                    var some = $("#myId").val();
                    alert($some);
            <?php  endif; ?>
            });

        });


    </script>
    </body>
</html>

但没有弹出警报框......我也试过这个:

<html>
    <head>
        <?php   echo $this->Html->script("jquery-1.4.3.min"); ?>

    </head>
    <body>
    <?php
        echo $content_for_layout;
    ?>

    <script type="text/javascript">
        $(document).ready(function () {
         //       alert('JQuery is succesfully included');

        $(':submit').click(function(){
            <?php if(($this->request->controller=='members')&&($this->action=='register')):?>
                    //alert('well');
                    var some = $("#myId").val();
                    alert($(some));
            <?php  endif; ?>
            });

        });


    </script>
    </body>
</html>

但它给了我一个警告框,显示 [object Object] .. :(

什么是正确的代码?

这就是我的 ctp 文件中的内容

<?php
    echo $this->Form->create('Member',array('action'=>'register','id'=>'myId'));
    echo $this->Form->input('username', array('id'=>'myId'));
    echo $this->Form->end('Check');
?>

这就是我在我的 html 源页面中得到的

好的

<head>

    <script type="text/javascript" src="/js/jquery-1.4.3.min.js"></script>      

</head>

<body>

<h2>REGISTER</h2>
用户名
<script type="text/javascript">

    $(document).ready(function () {

     //       alert('JQuery is succesfully included');



    $(':submit').click(function(){

                            //alert('well');

                var some = $("#myId").val();

                //alert(some);



                    });



    });





</script>

</body>

4

4 回答 4

0

这是满足您要求的脚本..

<HTML>
    <head>

    <script type="text/javascript">
    function show_ans(myform)
    {
        var answer=myform.ans.value;    
            alert(answer);
    }

    </script>
    </head>
    <body>
    <form onSubmit="show_ans(this)">
    <input type="text" id="ans" name="ans">
    <input type="submit">
    </form>
    </body>
    </HTML>
于 2013-07-19T07:37:53.707 回答
0

我已经让它工作了。好吧,id='myId' 是重复的……这是我的 ctp 文件。

<h2>REGISTER</h2>
<?php
    echo $this->Form->create('Member',array('action'=>'register','id'=>'myId'));
    echo $this->Form->input('username', array('id'=>'testMe'));
    echo $this->Form->end('Check');
?>

这是我的带有 jquery 的 default.ctp:

<html>
    <head>
        <?php   echo $this->Html->script("jquery-1.4.3.min"); ?>

    </head>
    <body>
    <?php
        echo $content_for_layout;
    ?>
    <script type="text/javascript">
        $(document).ready(function () {
         //       alert('JQuery is succesfully included');

        $(':submit').click(function(){
            <?php if(($this->request->controller=='members')&&($this->action=='register')):?>
                    //alert('well');
                    var some = $('#testMe').val();
                    //var some ='charm';
                    alert(some);
            <?php  endif; ?>
            });
        });
    </script>
    </body>
</html>
于 2012-05-09T00:43:01.507 回答
-1

服务器端代码未生成 id=myId 的输入元素。您需要重新评估 php 代码以输出正确的输入元素,如下所示:

<input type="text" id="myId" value="whatever" />

然后此代码将返回该输入的值:

$('#myId').val()

于 2012-05-08T09:52:13.610 回答
-1
var some = $("#myId").val();
alert(some);

Or
alert($("#myId").val());
于 2012-05-08T09:53:07.750 回答