JQuery将文本设置为输入文本字段

时间:2015-02-03 11:38:29

标签: javascript jquery html

我试图在输入文本字段中设置一些文本,但是不起作用。我做错了什么?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Page</title>
    </head>
    <body>
        <input class="link_input_text" value="" name="" type="text" />
    </body>
    <script type="text/javascript">
        $('link_input_text').val("Some text");
    </script>
</html>

谢谢!

3 个答案:

答案 0 :(得分:4)

变化:

$('link_input_text').val("Some text");

到此:

$('.link_input_text').val("Some text");

你必须使用类似CSS的选择器来选择jQuery中的元素。

答案 1 :(得分:1)

您必须使用jQuery Class Selector

使用此

$('.link_input_text').val("Some text");

而不是

$('link_input_text').val("Some text");

$(".link_input_text").val("Some Text");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input class="link_input_text" value="" name="" type="text" />

答案 2 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page</title>
</head>
<body>
<input class="link_input_text" value="" name="" type="text" />
</body>
<script type="text/javascript">
    $('.link_input_text').val("Some text");
</script>
</html>