Convert datalist option to json

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: "".

+4
source share
3 answers

Try using the selector $("input[list=partner]")by removing the trailing comma ,after$('#callbackurl').val()

$('#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": $("input[list=partner]").val(),
    "ips": [$('#ips').val()],
    "url": $('#callbackurl').val()
  };

  var json_server = JSON.stringify(form_server, null, 2);

  $('#myBtn2').after('<pre>' + json_server + '</pre>');

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<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>
Run codeHide result
+2
source

http://caniuse.com/#feat=datalist

DataList select.

<select name="partner" id="partner">
  <option value="">Please select</option>
  <option value="42">42</option>
  <option value="SMS">SMS</option>
  <option value="Online">Online</option>
</select>
0

id="partner" input datalist.
:

<input id="partner" list="partner" name="partner" type="text">
<datalist>
    <option value="42">42</option>
    <option value="SMS">SMS</option>
    <option value="Online">Online</option>
</datalist>  

:

<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>  

DEMO

id input,

<input id="partner12" 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>  

UpDated Demo

0

Source: https://habr.com/ru/post/1612757/


All Articles