【发布时间】:2015-01-23 21:18:42
【问题描述】:
我有几个在ko: foreach 绑定中动态生成的文本区域和单选按钮。当用户决定完成输入时,他们将单击“确定”按钮,该按钮将获取所有文本区域的输入和他们选择的单选按钮的值,并且它应该触发对服务器的 ajax 调用,因为输入需要存储在数据库中。每个 textarea 和 radiobutton 的值是分开存储的,因此需要将它们作为区分值发送到服务器。考虑到foreach 绑定,我遇到了很多麻烦,所以我不确定从哪里开始,因为文本区域没有可访问的唯一标识符,因为它们是使用foreach 生成的。谢谢。
所以,简单来说:
- 用户在多个文本区域中键入并单击单选按钮。
- 用户在完成输入后单击“确定”按钮。按钮单击功能检索所有文本区域和单选按钮值中的用户输入。
- 触发 ajax 调用并将值发送到服务器。
感谢任何帮助或链接到我可以关注的任何资源。我的谷歌搜索并没有完全回答我的问题。
图片:
服务器:
//retrieves form data from the client and serialized it
if (Request.HttpMethod == "POST")
{
// get json out of body
var serializer = new JsonSerializer();
var sr = new StreamReader(Request.InputStream);
var jtr = new JsonTextReader(sr);
dynamic data = serializer.Deserialize(jtr);
if (data.action == "getProjects")
{
getProjects(data);
}
}
通过 ajax 发布的示例对象:
Obj = {};
Obj.action = "getProjects";
Obj.list = arrayOfCheckboxValues;
查看:
<!-- ko foreach: projects -->
<div id="eachOppyProject">
<table>
<tbody>
<tr>
<td><a data-bind="attr: { href: '/tools/oppy/' + guid }"><span class="link" data-bind="value: guid, text: name"></span></a></td>
</tr>
<tr data-bind="text: projectDescription"></tr>
</tbody>
</table>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default" data-bind="visible: firstAnswers, click: function () { showBtnOK(); showDoneTA(); }">
<input type="radio" class="btn btn-default" />
Did it already
</label>
<label class="btn btn-default" data-bind="visible: firstAnswers, click: function () { showInterestedTA(); showBtnOK(); }">
<input type="radio" class="btn btn-default" />Didn't do it, but interested</label>
<label class="btn btn-default" data-bind="visible: firstAnswers, click: function () { neverInterested(); showBtnOK(); }">
<input type="radio" class="btn btn-default" />Never done it; not interested</label>
</div>
<div data-bind="visible: doneAnswer">
<textarea placeholder="Tell us a little of what you've done. Like, when did you do it? Who was in charge of it? Things like that."
class="form-control newSessionAnalyst" data-bind="textInput: doneProject, attr: { id: guid, name: guid + 'doneProject' }"
/>
<textarea placeholder="If there's anything else you'd like us to know, tell us here."
class="form-control newSessionAnalyst" data-bind="textInput: doneProjectComment, attr: { id: guid, name: guid + 'doneProjectComment' }"/>
</div>
<div data-bind="visible: interestedAnswer">
<textarea placeholder="So, you're interested, huh? Tell us why."
class="form-control newSessionAnalyst" data-bind="attr: { id: guid, name: guid + 'interestedProject' }"/>
<textarea placeholder="If there's anything else you'd like us to know, tell us here."
class="form-control newSessionAnalyst" data-bind="attr: { id: guid, name: guid + 'interestedProjectComment' }"/>
</div>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default" data-bind="visible: doneAnswer, click: function () { showBtnOK(); showInterestedMoreTA(); }">
<input type="radio" class="btn btn-default" />
Interested in doing more</label>
<label class="btn btn-default" data-bind="visible: doneAnswer, click: function () { showBtnOK(); notInterestedMore(); }">
<input type="radio" class="btn btn-default" />
No plans to do this again</label>
</div>
<div data-bind="visible: interestedMore">
<textarea placeholder="You want to do more? Way to go! Tell us more!"
class="form-control newSessionAnalyst" data-bind="attr: { id: guid, name: guid + 'interestedMore' }, value: $parent.saved_value"/>
<textarea placeholder="If there's anything else you'd like us to know, tell us here."
class="form-control newSessionAnalyst" data-bind="attr: { id: guid, name: guid + 'interestedMoreComment' }"/>
</div>
<div style="text-align: right;">
<input type="button" data-bind="visible: btnOK, click: function () { clearView(); }" value="OK" class="btn btn-default "/><br /><br />
</div> //this is the button that captures all input
</div>
<!-- /ko -->
查看模型:
function ViewModel(proj) {
var self = this;
var wrappedProjects = proj.map(function (p) {
return new Project(p);
});
self.projects = ko.observableArray(wrappedProjects);
}
function Project(proj) {
var self = proj;
self.firstAnswers = ko.observable(true);
self.doneAnswer = ko.observable(false);
self.showDoneTA = function () {
self.doneAnswer(true);
self.interestedAnswer(false);
}
self.interestedAnswer = ko.observable(false);
self.showInterestedTA = function () {
self.interestedAnswer(true);
self.doneAnswer(false);
self.interestedMore(false);
}
self.interestedMore = ko.observable(false);
self.showInterestedMoreTA = function () {
self.interestedMore(true);
}
self.notInterestedMore = function () {
self.interestedMore(false);
}
self.neverInterested = function () {
self.doneAnswer(false);
self.interestedAnswer(false);
self.interestedMore(false);
}
self.btnOK = ko.observable(false);
self.showBtnOK = function () {
self.btnOK(true);
console.log(self.btnOK());
}
self.savedMSG = ko.observable(false);
self.clearView = function () {
self.firstAnswers(false);
self.doneAnswer(false);
self.interestedAnswer(false);
self.interestedMore(false);
self.btnOK(false);
self.savedMSG(true);
}
self.showFirstAnswers = function () {
self.firstAnswers(true);
self.savedMSG(false);
}
return self;
}
【问题讨论】:
标签: javascript jquery html knockout.js textarea