1

I want to get input of every form in my page that their name is name my forms are like this

<form class="form" role="form" onsubmit="completeSave(this)">
      <div class="form-body">
           <div class="form-group">
               <label>name</label>
               <input type="text" class="form-control" name="name"
                                               value="{{$media->name}}">
            </div>
       </div>
 </form>
// next form!
 <form class="form" role="form" onsubmit="completeSave(this)">
      <div class="form-body">
           <div class="form-group">
               <label>name</label>
               <input type="text" class="form-control" name="name"
                                               value="{{$media->name}}">
            </div>
       </div>
 </form>

I already have some code that do something with each form (an ajax request ) but I want to have a button in a page that submit all forms data

 <button class="btn btn-primary" onclick="completeSaveAll()">submit</button>

i tried this javascript code :

 function completeSaveAll() {
        var form = $('form');
        form.each(function () {
            console.log(this.find("[name=name]"));
        })
  } 

but it doesn't work how can I do it?

Thielicious
  • 4,122
  • 2
  • 25
  • 35
mohammad
  • 47
  • 1
  • 6

5 Answers5

0

Try serializeArray()

$.ajax({
type : 'POST',
url : 'url',
data : $('#form').serializeArray()
});
Kinshuk Lahiri
  • 1,468
  • 10
  • 23
0

Other than this previously answered similar question

I would say your onclick handler might not be able to initialize successfully as you are using jQuery $ in the handlerFunction completeSaveAll and DOM elements might render before jQuery is fetched.

Community
  • 1
  • 1
atulj
  • 71
  • 2
  • 4
  • 10
0

HTML:

<form class="form" role="form">
      <div class="form-body">
           <div class="form-group">
               <label>name</label>
               <input type="text" class="form-control" name="name" value="name1">
            </div>
       </div>
 </form>
// next form!
 <form class="form" role="form">
      <div class="form-body">
           <div class="form-group">
               <label>name</label>
               <input type="text" class="form-control" name="name" value="name2">
            </div>
       </div>
 </form>
 <button class="btn btn-primary">submit</button>

jQuery:

Use submit and prevent the default process, so that only the values will be returned.

$('form').on('submit', function(e) {
  e.preventDefault();
  var form = $(this);
  form.each((key, val)=>
    console.log(val.name.value))
})

And then trigger this above using your button outside of these forms

$('button').click(()=> 
    $('form').trigger('submit')
)

Replace the values with {{$media->name}} or whatever you want to do.

Demo here

Thielicious
  • 4,122
  • 2
  • 25
  • 35
0

Its very simple:

'use strict';
var name = $('input[name="name"]');
$.each(name , function(i,el){
  console.log(i);
});

Here you go jsbin for it

https://jsbin.com/pewulimefu/2/edit?html,js,output

panatoni
  • 735
  • 6
  • 13
-1

Try This:

function completeSaveAll() {
  debugger
        //var form = $('form');
        $('form').each(function () {
            console.log($(this).find("[name=name]").val());
        })
  } 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form class="form" role="form" onsubmit="completeSave(this)">
      <div class="form-body">
           <div class="form-group">
               <label>name</label>
               <input type="text" class="form-control" name="name"
                                               value="{{$media->name}}">
            </div>
       </div>
 </form>
// next form!
 <form class="form" role="form" onsubmit="completeSave(this)">
      <div class="form-body">
           <div class="form-group">
               <label>name</label>
               <input type="text" class="form-control" name="name"
                                               value="{{$media->name}}">
            </div>
       </div>
 </form>
<button class="btn btn-primary" onclick="completeSaveAll()">submit</button>
Kumar_Vikas
  • 837
  • 7
  • 16