获取输入框的值

时间:2015-03-12 17:26:39

标签: javascript jquery

我试图用这个输入框做任何事情,但在这里我试图提取当前文本。



console.log('running');
$('#kw-search').on('keyup', function (event) {
	var name = $(this).val();
	alert(name);
});

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8"/>
		<title>Keylines playaround</title>
		<link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700" rel='stylesheet' />
		<!-- <link href="css/main.css" rel="stylesheet" />
		<link href="css/uploader_style.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/jquery.qtip.min.css"> -->
		<!-- jquery  -->
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
		<!-- jQuery File Upload Dependencies -->
		<!-- <script src="js/jquery.knob.js"></script>
		<script src="js/jquery.ui.widget.js"></script>
		<script src="js/jquery.iframe-transport.js"></script>
		<script src="js/jquery.fileupload.js"></script>
		<script src="js/jquery.filereader.js"></script>
		<script src="js/jquery.qtip.min.js"></script> -->
		<!-- Keylines -->
		<!-- <script type="text/javascript" src="js/keylines.js"></script> -->
		<!-- <script type="text/javascript" src="js/keylines.setup.js"></script> -->
		<!-- google table -->
		<!-- <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['table']}]}"></script>
		<script type="text/javascript" src="js/google-api.draw-tables.js"></script> -->
		<!-- <script src="js/jquery.csv-0.71.js"></script> -->
		<!-- Our main JS files -->
		<!-- <script src="js/uploader.js"></script> -->
		<script src="js/search.live.js"></script>
	</head>

	<body>
		<div id="commands">
			<input type="text" id="kw-search">
		</div>
		<div id="phrases">
			
		</div>
		<div id="keywords">
			
		</div>
		<div id="file-upload">
			<div class="panel">
				<form id="upload" method="post" action="\cgi-bin\keylines\upload.py" enctype="multipart/form-data">
					<div id="drop">
						Drop Files Here

						<a>Browse</a>
						<input type="file" name="upl" multiple />
					</div>

					<ul>
					</ul>
				</form>
			</div>
		<div class="pull-me">File Upload</div>
		</div>
		<div id="container">
			<div id="kl-container">
				<div id="kl" style="width: 1000px; height: 800px;" ></div>
			</div>
		</div>
	</body>
</html>
&#13;
&#13;
&#13;

显示的javascript来自search-live.js。它适用于此页面,但拒绝在我的服务器上。 上面的大部分代码都已被注释掉,只是为了获取输入和上传器。 有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您需要将代码移动到页面末尾,或将其包装在document ready处理程序中:

$( document ).ready(function() {
  // Handler for .ready() called.
});

$(function() {
  // Handler for .ready() called.
});

您正尝试在此时不存在的元素上执行代码。

答案 1 :(得分:1)

你应该确保在加载元素kw-search之前没有执行代码,所以将它包装在文档读取函数中会阻止这种情况发生

$( document ).ready(function() {
    console.log('running');
    $('#kw-search').on('keyup', function (event) {
    var name = $(this).val();
    alert(name);
    });
});