javascript - Convert datalist option to json -
i have form , have variables include in json object. created function 1 of form fields datalist variable , can't find way covert json. 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 looks this:
<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 partner if not on 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 choose option list , press submit button receive correct json billing name empty: " ".
try using selector $("input[list=partner]")
, removing trailing comma ,
following $('#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 partner if not on 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>
Comments
Post a Comment