JQuery - 单击"提交"按钮获取表单值

use*_*853 4 javascript forms jquery dom

我有以下功能,我想要做的就是从表单字段中获取值.

$( ".searchbutton" ).click(function() {
    var tc = $(this).closest("form input[name='searchbox']").val();
    alert(tc);      
    return false;
}); 
Run Code Online (Sandbox Code Playgroud)

警报一直告诉我"未定义".我最亲近的,父母,父母,发现等等.我不知道我做错了什么.我点击提交按钮,我想要的回报是搜索框中的值.请帮忙.

HTML

<form action="/index.php" method="get" class="qsearch" >
<input type="text" id="fsearch" name="searchbox" >
<input class="searchbutton" type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)

Vla*_*chi 7

试试这个:

$( ".searchbutton" ).click(function() {
    var tc = $(this).closest("form").find("input[name='searchbox']").val();
    alert(tc);      
    return false;
}); 
Run Code Online (Sandbox Code Playgroud)

更新是的 ,它适用于您的HTML - 请参阅此处http://jsfiddle.net/qa6z3n1b/

作为替代 - 您必须使用

$( ".searchbutton" ).click(function() {
    var tc = $(this).siblings("input[name='searchbox']").val();
    alert(tc);      
    return false;
}); 
Run Code Online (Sandbox Code Playgroud)

在你的情况下.http://jsfiddle.net/qa6z3n1b/1/