JQuery 从所有输入创建数组并提取它

JQuery Creating Array from All Inputs and Extracting It

本文关键字:数组 提取 创建 输入 JQuery      更新时间:2023-09-26

这是我的小提琴

这是我的输入代码:

<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">