我使用 knockout.js 和映射插件映射了我的表单。我准备将表单保存为 json 并将其发送回服务器。这是我第一次这样做,那么最简单的方法是什么?
这是我到目前为止所拥有的:
// Here's my data model
var viewModel;
$.getJSON('/myJSONdata', function (data) {
viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
//convert mapped data to json format
var jsonData = ko.mapping.toJSON(viewModel);
// Do something to send the form data in json format back to the server on form submit
<form data-bind="submit: doSomething">
<label for="typeOfIncident">Do you agree?</label>
<label>
<input type="radio" name="doYouAgree" value="Yes" data-bind="value: doYouAgree" checked>
Yes
</label>
<label>
<input type="radio" name="doYouAgree" value="No" data-bind="value: doYouAgree">
No
</label>
<!-- submit button -->
<button type="submit">Submit</button>
</form>