I have a form and I have some variables that I would like to include in a json object. I created a function that does this, but one of my form fields is a datalist variable, and I cannot find a way to hide it before Json. This is the function:
$('#myBtn2').click(function() {
var form_server = {
"id": JSON.parse($('#id').val()),
"type": "service",
"name": $("#name").val(),
"msg_types": [6,7,8,9],
"billing_id": JSON.parse($('#billing_id').val()),
"billing_name": $("#partner").val(),
"ips": [$('#ips').val()],
"url": $('#callbackurl').val(),
};
var json_server = JSON.stringify(form_server, null, 2);
$('#myBtn2').after('<pre>' + json_server + '</pre>');
});
The HTML form is as follows:
<label for="id">ID: </label>
<input id="id" name="id" type= "number"/><br>
<label for="name">Service name: </label>
<input id="name" name="name" type="text" /><br>
<br><label for="billing_id">Billing ID (Partner ID/GUID): </label>
<input id="billing_id" name="billing_id" type= "number"/><br>
<label for="ips">IP addresses: </label>
<input id="ips" name="ips" type="text" /><br>
<label for="url">Callback URL: </label>
<input id="callbackurl" name="callbackurl" type="url" /><br>
<input name="myBtn2" id="myBtn2" type="button" value="Submit Data" />
<label>Partner Name</label><br>Please add a partner if not on the list bellow<br>
<input list="partner" name="partner" type="text">
<datalist id="partner">
<option value="42">42</option>
<option value="SMS">SMS</option>
<option value="Online">Online</option>
</datalist><br>
When I select an option from the list and click the submit button, I get the correct JSON, but the billing name is empty: "".
source
share