简体   繁体   English

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

[英]JQuery - Click Submit Button Get Form Value

I have the following function and all i am trying to do is get the value out of the form field. 我有以下功能,我想要做的就是从表单字段中获取值。

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

The alert keeps telling me "Undefined". 警报一直告诉我“未定义”。 I have treid closest, parent, parents, find, etc. I don't know what im doing wrong. 我有最近的亲戚,父母,父母,发现等等。我不知道我做错了什么。 Im clicking the submit button and all i want in return is the value in the search box. 我点击提交按钮,我想要的回报是搜索框中的值。 Please help. 请帮忙。

html HTML

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

Try this: 尝试这个:

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

Update Yep, it work with your HTML - see here http://jsfiddle.net/qa6z3n1b/ 更新是的 ,它适用于您的HTML - 请参阅此处http://jsfiddle.net/qa6z3n1b/

As alternative - you must use 作为替代 - 您必须使用

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

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

Try easiest way: 尝试最简单的方法:

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

How about just using $('input[name="searchbox"]') selector: 如何使用$('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