我正在尝试使用HTML从已发布的表单中获取单个表单值,Jquery可以让任何人告诉我我在使用以下脚本时出错了。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<form id="target" action="1.html">
<input type="text" value="Hello world">
<input type="submit" value="Go">
</form>
<script>
$( "#target" ).submit(function( event ) {
var input = $("#target :input[text]");
alert( input);
console.log(input)
event.preventDefault();
});
</script>";
</body>
</html>
我似乎只能返回一个对象,但我无法使用alert或console.log功能显示它。
答案 0 :(得分:2)
var input = $("#target :input[text]");
应该是:
var input = $("#target input[type='text']").val();
答案 1 :(得分:0)
将提交事件包裹在准备好的功能中
$(document).ready(function(){
$( "#target" ).submit(function( event ) {
var input = $("#target input[text]").val();
alert( input);
console.log(input)
event.preventDefault();
});
});