Using jQuery/jQueryUI I want to populate a form using this HTML/JS show below.
The url "editController/loadContents" will return some data and .html will (I believe) populate the form based upon the data but what structure should the data have ?.
The only examples I can find in the jQueryUI doco are for single element forms.
My guess is that some JSON which looks like this ...
{
"starttime": "10:00",
"endtime": "11:00",
}
... would populate the input fields. But how are the OPTIONS for the SELECTs provided and one of the the OPTIONs specified as 'selected' ?
<div id="dialog" title="Basic dialog">
<!-- loaded from ajax call -->
<form id="exampleForm">
<fieldset>
<label for="activity">Activity</label>
<br />
<select name="activity" id="activity" class="ui-widget-content ui-corner-all">
</select>
<br />
<label for="subactivity">Sub-Activity</label>
<br />
<select name="subactivity" id="subactivity" class="ui-widget-content ui-corner-all">
</select>
<br />
<label for="activity">Reason</label>
<br />
<select name="reason" id="reason" class="ui-widget-content ui-corner-all">
</select>
<br />
<label for="starttime">Start</label>
<br />
<input type="text" name="starttime" id="starttime" class="text ui-widget-content ui-corner-all" />
<br />
<label for="endtime">End</label>
<br />
<input type="text" name="endtime" id="endtime" class="text ui-widget-content ui-corner-all" />
<br />
</fieldset>
<input type="button" onclick="Save()" />
</form>
</div>
<script>
$(function() {
$('.myPop').click(function() {
$.get("editController/loadContents", function(data){
$("#dialog").html(data);
});
$("#dialog").dialog('open');
});
});
function Save(){
$.post("/editController/Edit", $("#exampleForm").serialize(),
function(data){
$("#dialog").dialog('close');
//update grid with ajax call
});
}
</script>
BTW I've adapted this code from the very useful answer at How to use a jQuery UI Modal Form from ASP.Net MVC list page