开发者

error: when i try to add <form tag

below is my html form and i dont get any error if i dont have a <form tag, so i add a form tag like this:

开发者_运维百科
<form class="cmxform" id="commentForm" method="post" action="">

get this error:

Microsoft JScript runtime error: 'first_name' is undefined

when i try to read the value of a text box:

First Name:
<input id="first_name" class=" text hasCorners required" maxlength="200" 
name="first_name"   />

function PostData() {
        var _firstName = first_name.value;
        ......
        ......

 $(document).ready(function () {

            $('#btnRegister').click(function () {
                PostData();
            });


Try:

var _firstName = $('#first_name').val(); 


Seems like you need to specify the form element's ID first:

commentForm.first_name.value;

Not sure why though.


EDIT: To clarify, this resolves the IE issue with the technique you were using. As @bobince noted in the comment below, accessing IDs as global properties is not safe unless you're targeting specific browsers.

Based on the code in your question and your comment under another answer, it appears that you already know how to select by ID using jQuery and getElementById.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜