So, I have a group of key value pairs on a document. They are sets of 2 inputs. I'll also need to reject any non-pairs, so if there is only a key or only a value, I don't want one of those sides included.
Like this, input1 is key, input2 is value:
<div id = "group_o_inputs">
<div class="ind_form">
<input class="input1" type="text"><input class="input2" type="text">
</div>
<div class="ind_form">
<input class="input1" type="text"><input class="input2" type="text">
</div>
<div class="ind_form">
<input class="input1" type="text"><input class="input2" type="text">
</div>
</div>
How could I extract these, I am thinking of putting them into a json array. I am thinking of a syntax like this:
var items1 = $("#group_o_inputs .ind_form .input1");
var items2 = $("#group_o_inputs .ind_form .input2");
Then I can use the two arrays with a for loop or something? And like I mentioned, since the user can leave one input out by accident, I'd like to account for that. Eventually it would come out like this:
{
key: value, key: value, key: value, key: value
}
Iterate the ind_form
class and map an array of objects
var data = $('.ind_form').has('.input1,.input2').map(function(){
return {
key : $(this).find('.input1').val(),
value : $(this).find('.input2').val()
}
}).get();
Produces
[
{key: 'input1-1 value', value : 'input2-1 value'},
{key: 'input1-2 value', value : 'input2-2 value'}
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.