JQuery 从所有输入创建数组并提取它
JQuery Creating Array from All Inputs and Extracting It
这是我的小提琴
这是我的输入代码:
<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 中的这些数组中提取所有值?
这是
最简单的方法:-
<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>
尝试使用 $.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);
});
<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">
为什么不使用一个简单的选择器和每个?
$('#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">
相关文章:
- Regex提取URL返回数组的一部分;未定义”;
- 如何从合并的结果集中提取数组
- 如何从数组中提取对象
- 从多维数组javascript中提取特定值
- 从数组中提取特定信息
- 使用JavaScript在Json中提取时,将数组的元素转换为String
- 正在复制数组并提取
- 在Jquery中从JSON中提取数组值
- 如何将json文件中的数据提取到对象数组中,并在两个控制器之间共享
- 使用从forEach循环中提取的国家代码从数组中获取国家名称
- 如何在javascript中使用localStorage从数组中存储和提取数据
- 循环遍历数字和字母数组并仅提取数字
- 如何从..中提取信息..我称之为数组
- 如何从生成的JSON数组中提取JSON对象
- 在jquery中使用.split并从数组中提取第一个项
- 在提取dom对象后,无法将每个名为tab的元素推送到数组中
- 使用jsRender,如何使用有序位置以外的东西从数组中提取特定项
- 创建新的javascript数组,从对象属性中提取数据
- 从SQL中提取一个表来创建javascript数组
- Javascript数组提取重复到新的数组