简体   繁体   中英

Not able to convert form data into JSON object using form2js.js

I am using form2js.js for converting my form data into a JSON object.

Here is the code:

var loginData = form2js('loginForm', '.', true);
console.log("FormData=" + JSON.stringify(loginData));

But the JSON String is empty. loginform is the ID of my form. I have searched for help on but I am not getting anything.

Here is the HTML form:

<form action="index.html" class="padder" id="loginForm" name="loginForm">                                
   <label class="control-label">User</label>
   <input id="userId" type="text" placeholder="User ID" class="form-control">                               
   <input type="password" id="password" placeholder="Passcode" class="form-control"> 
   <button type="submit" id="signInBtn" class="btn btn-info">Sign in</button>                               
</form>

The problem seems to be the missing name attribute

<form action="index.html" class="padder" id="loginForm" name="loginForm">
    <label class="control-label">User</label>
    <input name="userId" id="userId" type="text" placeholder="User ID" class="form-control" />
    <input name="password" type="password" id="password" placeholder="Passcode" class="form-control" />
    <button type="submit" id="signInBtn" class="btn btn-info">Sign in</button>
</form>

Demo: Fiddle

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM