代码:
<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时,有没有人知道如何得到值?
发布于 2013-09-11 18:25:43
尝试:
$('form').serialize();或者:
var values = [];
$('input').each(function() {
values.push($(this).val());
});发布于 2013-09-11 18:26:34
看起来你在使用jQuery。
$('form[name="newForm"]').serializeArray();这将产生一个数组,其外观如下:
[{name:'test1', value:'value1'},...]发布于 2013-09-11 18:41:17
jsFiddle:http://jsfiddle.net/Yw4Hc/3/
$('input.form-control').each(function() {
alert($(this).attr('name') + " = " + $(this).val());
});https://stackoverflow.com/questions/18748538
复制相似问题