这是我的Fiddle
这是我的输入代码:
<input type="text" id="first" name="first">
<input type="text" id="second" name="second">
<input type="text" id="third" name="third">
<input type="text" id="fourth" name="fourth">
<input type="button" id="driver" name="driver" value="Submit">
这是我的剧本:
<script>
$(document).ready(function() {
$("#driver").click(function(event) {
var A = $("#first").val();
var B = $("#second").val();
var C = $("#third").val();
var D = $("#fourth").val();
console.log(A);
console.log(B);
console.log(C);
console.log(D);
});
});
</script>
很少有小提琴可以用很少的复杂方式创建一个数组,即
$('document').ready(function() {
var results = [];
var items = $('[name^=item]');
$.each(items, function(index, value) {
value = $(value);
var jsObject = {};
jsObject[value.attr('name')] = value.attr('value');
results.push(jsObject);
});
console.log(results);
});
但是有一种简单的方法来创建一个包含所有元素的数组并从JQuery中的那些数据中提取所有值吗?
答案 0 :(得分:3)
这是最简单的方法: -
<script>
$(document).ready(function() {
var arr=[]; //you can make 'arr' as local or global as you want
$("#driver").click(function(event) {
event.preventDefault(); //disable default behaviour of #driver
var A = $("#first").val();
var B = $("#second").val();
var C = $("#third").val();
var D = $("#fourth").val();
arr.push(A); //store values in array with .push()
arr.push(B);
arr.push(C);
arr.push(D);
console.log(arr); //print array
});
});
</script>
答案 1 :(得分:2)
尝试使用$.map()
代替array.push()
var textboxes;
var extract = function() {
var arr = textboxes.map(function() {
return this.value; //textbox value
}).get(); //get array
console.log('text boxe values: ', arr);
};
$(function() {
textboxes = $('input:text'); //get all text boxes
$('#driver').on('click', extract);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="first" name="first">
<input type="text" id="second" name="second">
<input type="text" id="third" name="third">
<input type="text" id="fourth" name="fourth">
<input type="button" id="driver" name="driver" value="Submit">
&#13;
答案 2 :(得分:1)
为什么不使用简单的选择器呢?
$('#driver').on('click', function() {
var fields = [];
$('input, select').each(function() {
if ($(this).is('[type=checkbox]')) {
if ($(this).is(':checked')) {
fields.push($(this).val());
}
} else if ($(this).is('[type=radio]')) {
if ($('[name=' + $(this).attr('name') + ']:checked').get(0) === this) {
fields.push($(this).val());
}
} else if (!$(this).is('[type=submit], [type=button]')) {
fields.push($(this).val());
}
});
console.log(fields);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="first" name="first">
<br />
<input type="checkbox" value="fineprint" />Did you read the print?
<br />
<input type="text" id="second" name="second" />
<br />
<select>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
<br />
<input type="text" id="third" name="third" />
<br />Test?
<br />
<input type="radio" name="bool" value="yes" checked />Yes
<br />
<input type="radio" name="bool" value="no" />No
<br />
<input type="text" id="fourth" name="fourth" />
<br />
<input type="button" id="driver" name="driver" value="Submit">