繁体   English   中英

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

[英]JQuery - Click Submit Button Get Form Value

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

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

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

HTML

<form action="/index.php" method="get" class="qsearch" >
<input type="text" id="fsearch" name="searchbox" >
<input class="searchbutton" type="submit" value="Submit">
</form>

尝试这个:

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

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

作为替代 - 您必须使用

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

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

尝试最简单的方法:

<script>
$( ".searchbutton" ).click(function() {
var tc = $('#fsearch').val();
alert(tc);      
return false;
}); 
</script>

如何使用$('input[name="searchbox"]')选择器:

$( ".searchbutton" ).click(function() {
    var tc = $('input[name="searchbox"]').val();
    alert(tc);      
    return false;
}); 

暂无
暂无

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

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