简体   繁体   中英

“Form is not defined” error using jQuery

I am learning jquery and am writing some very simple jquery code that posts some variables to a method in my controller. I get “myform is not defined” error in firebug when using the code posted below.

Here is my html:

<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="assets/js/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        function get(){
            $.post('jqtest/test',{name: myform.myname.value},
            function (output){
                $('#username').html(output).show();          
            }    
        );
        }
    </script>
</head>

<body>
    <div>TODO write content</div>
    <form name="myform">
        <input type="text" name="myname">
        <input type="button" value="Get" onclick="get();">
    </form>
    <div id="username"></div>
</body>

Here is my codeigniter controller code:

class Jqtest extends CI_Controller {

function _Jqtest() {
    parent::controller();
}

function index() {
    $this->load->view("jqtest/jqtest.html");
}

function test() {
    print_r($_POST);
    $this->load->view("jqtest/jqtest.html");
}

}

When the Get button is clicked, the test method should print the POST values. When I run the code in FireFox, it's not working (nothing is getting printed out). When I checked the firebug console, I found a "myform is not defined" error.

I know there is something wrong with my jQuery get() function, can anybody please help me out, thank you very much!

"myform is not defined" error occures because you have not set myform in javascript.

try Jquery's serializeArray()

var myform= $('form').serializeArray();

function get(){
            $.post('jqtest/test',{name: myform.myname.value},
            function (output){
                $('#username').html(output).show();          
                }   );
}
    function get(){
        $.post('jqtest/test',{name: $('form[name="myform"] input[name="myname"]').val()},
        function (output){
            $('#username').html(output).show();          
        }    
    );
    }

Replace

myform.myname.value

with

$('input[name="myname"]').val()

您可以简单地做到这一点:

document.myform.myname.value

Form should have an id <form name="myform" id="myformid">

$.post('jqtest/test',{name: myform.myname.value},

would replaced with the below code :)

$.post('jqtest/test',{name: $( 'input[name="myname"]' ).val()},

If u need to send the all form value then u should use serialize. it's faster.

API DOC: serialize

If u use same same input name for different form. then u should have to write as below:

var $form = $( "#myformid" );
name = $form.find( 'input[name="myname"]' ).val();

and post will be:

$.post('jqtest/test',{name: name},

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.

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