我试图创建一种通过jQuery从具有相同名称的字段捕获信息的方法。以下是表单代码的示例...
$( "#submit" ).click(function() {
var field1 = $("input[name='field1[]']")
.map(function(){return $(this).val();}).get();
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Text input-->
<form >
<div class="form-group">
<label for="field1">Field 1:</label>
<input type="text" class="form-control" id="field1" name="field1[]">
</div>
<div class="form-group">
<label for="field1">Field 1:</label>
<input type="text" class="form-control" id="field1" name="field1[]">
</div>
<div class="form-group">
<label for="field1">Field 1:</label>
<input type="text" class="form-control" id="field1" name="field1[]">
</div>
<div class="form-group">
<label for="sel1">Select list:</label>
<select class="form-control" id="field1" name="field1[]">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</div>
<button type="submit" class="btn btn-default" id="submit">Submit</button>
<!-- <input type="submit" value="Submit"> -->
</form>
&#13;
我的问题是如何将选择框包含在变量field1
中。
答案 0 :(得分:0)
只需将select[name='field1[]']
添加到您的选择器并将其与昏迷分开,如下所示:
$( "#submit" ).click(function() {
var field1 = $("input[name='field1[]'], select[name='field1[]']")
.map(function(){return $(this).val();}).get();
});
有关多个选择器的更多信息:https://api.jquery.com/multiple-selector/