2017-07-27 44 views
0

嘿,我有一個表單,輸出保存在數據庫中。 我想要的是將其保存在json字符串中並顯示字符串。從表單輸入生成json字符串

<form> 
    First name:<br> 
    <input type="text" name="firstname"> 
    <br> 
    Last name:<br> 
    <input type="text" name="lastname"> 
    <select name="gender" > 
     <option value=""></option> 
     <option value="Man">Man</option> 
     <option value="Vrouw">Vrouw</option> 
    </select> 
    adress:<br> 
    <input type="text" name="adress"> 
    <input type="submit" class="submit" value="Submit!" /> 
</form> 


    { 
    "PersonalData" : { 
     "Gender" : [value of gender] 
     "FirstName" : "[valeu of firstname]", 
     "adress" : "[valeu of adress]", 
} 
+1

您所使用的編程語言? – gdmanandamohon

+1

這已經在這裏得到解答:https://stackoverflow.com/questions/1184624/convert-form-data-to-javascript-object-with-jquery – MrMadsen

+1

我使用經典的asp,但我希望它與javascript – curo

回答

0

創建一個JSONObject並使用put()方法添加所需的所有值。然後你可以使用.toString()函數把它變成一個字符串。

+2

' JSON'是一種字符串格式,不是JavaScript對象 – guest271314

+0

Java中有一個名爲JSONObject的類,假設它在這裏很有用並且可用。 – GorkemSahin

0

$(document).ready(function(){ 
 
    var result = {}; 
 
    var PersonalData = {}; 
 
    
 
    $("#submitt").click(function(){ 
 
    PersonalData.FirstName = $('#first').val(); 
 
    PersonalData.Address = $('#address').val(); 
 
    PersonalData.Gender = $("#gender option:selected").text(); 
 
    
 
    result.PersonalData = PersonalData; 
 
    console.log(result); 
 
    var resultString = result.toString(); 
 
    console.log(resultString); 
 
    }); 
 
    
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div> 
 
    First name:<br> 
 
    <input type="text" name="firstname" id="first"> 
 
    <br> 
 
    Last name:<br> 
 
    <input type="text" name="lastname"> 
 
    <select name="gender" id="gender" > 
 
     <option value=""></option> 
 
     <option value="Man">Man</option> 
 
     <option value="Vrouw">Vrouw</option> 
 
    </select> 
 
    <br> 
 
    Address:<br> 
 
    <input type="text" name="adress" id="address"> 
 
    <input type="submit" class="submit" value="Submit!" id="submitt" /> 
 
</div>