简体   繁体   English

表单输入类型文本jQuery

[英]form input type text jQuery

I do not know why I cannot get access to the input text when the code does not have any error. 我不知道为什么在代码没有任何错误的情况下我无法访问输入文本。

<form action="#">
                <table width="80%" align="center">

                    <tr>
                        <td>Write your name</td>
                        <td><input type="text" name="yourName" placeholder="Name" /></td>

                        <td><input type="button" value="Submit" id="sendName"/></td>
                    </tr>
                </table>

            </form>
            <p />
            <div id="nameResult"></div>

and the js (jQuery): 和js(jQuery):

$(document).ready(function(){

    $('#sendName').click(function(){
            $(document).ready(function(){

                var tag = $('#yourName').val();

                $('<p>Name ' + tag + '</p>').appendTo('#nameResult');

            });
    }); 

})

The output is always undefined. 输出始终是未定义的。 I do not know what I am doing wrong. 我不知道我在做什么错。

You are calling name as if it is an id using # . 您正在使用#来调用name ,就好像它是一个id

Change this 改变这个

<input type="text" name="yourName" placeholder="Name" />

to this, in which the input control has an id defined: 为此,其中输入控件具有定义的id

<input type="text" id="yourName" name="yourName" placeholder="Name" />

If you prefer to stick to name and do not want to define an id , then you can do this as well: 如果您希望stick使用名称并且不想定义id ,那么也可以这样做:

var tag = $('input[name="yourName"]').val();

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

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