代码:
<form name="newform" action="test.php" method="post" class="form-horizontal input-prepend" id="newform">
<div class="form-group">
<label class="control-label" for="test1">test1</label>
<input type="text" name="test1" class="form-control" id="test1" placeholder="">
</div>
<div class="form-group">
<label class="control-label" for="test2">test2</label>
<input type="text" name="test2" class="form-control" id="test2" placeholder="">
</div>
<div class="form-group">
<label class="control-label" for="test3">test3</label>
<input type="text" name="test3" class="form-control" id="test3" placeholder="">
</div>
<div class="form-group">
<label class="control-label" for="test1">test4</label>
<input type="text" name="test4" class="form-control" id="test4" placeholder="">
</div>
<div class="form-group">
<label class="control-label" for="test5">test5</label>
<input type="text" name="test5" class="form-control" id="test5" placeholder="">
</div>
</form>
为了获取输入的值,我们可以使用其id
,例如alert($("#test5").val())
,但是如何当我们不知道输入的id
时,从表单的开头顺序获取输入值?
对于我的测试代码,我可以使用代码获取值:
var value1 = $("test1").val();
var value2 = $("test2").val();
var value3 = $("test3").val();
var value4 = $("test4").val();
var value5 = $("test5").val();
有人知道当我们不知道输入的 id
时如何获取值吗?
最佳答案
尝试:
$('form').serialize();
或者:
var values = [];
$('input').each(function() {
values.push($(this).val());
});
关于jquery - 如何获取表单中所有输入的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18748538/