我想基于按钮点击显示文本区域。非常简单,但textarea和按钮是使用Knockout js动态生成的。我当前的代码有效,除了它只扩展了第一个文本区域。显示了几个项目。
HTML(按钮和textarea是最后两个控件):
<!-- ko foreach: projects -->
<div id="eachOppyProject" style="border-bottom: 1px solid #eee;">
<table>
<tbody>
<tr>
<td><a data-bind="attr: { href: '/tools/oppy/' + guid }" style="font-size: 25px;"><span class="link" data-bind=" value: guid, text: name"></span></a></td>
</tr>
<tr data-bind="text: projectDescription"></tr>
<tr data-bind="text: guid"></tr>
</tbody>
</table>
<span class="forminputtitle">Have you done project this before?</span>
<input type="button" id="oppyBtn" class="btnOppy" data-bind="click: toggleTextArea" value="Yes" />
<textarea id="oppyDoneTextArea" placeholder="Tell us a little of what you've done." data-bind="visible: show" /><br />
</div>
<!-- /ko -->
JavaScript的:
function displayTextArea() {
var my_disply = document.getElementById('oppyDoneTextArea').style.display;
if (my_disply == "block")
document.getElementById('oppyDoneTextArea').style.display = "none";
else
document.getElementById('oppyDoneTextArea').style.display = "block";
}
淘汰视图模型:
function ProjectViewModel(proj) {
//console.log(proj);
var self = this;
self.projects = ko.observableArray(proj);
var project = function(){
var self = this;
self.show = ko.observable(false);
self.toggleTextArea= function(){
self.show(!self.show());
};
};
};
如您所见,控件是基于Knockout绑定的对象动态生成的。因此,使用ID是一个坏主意,因为它会生成重复的ID。这是我现在的问题 - 这段代码适用于第一个文本区域,但不适用于显示的其他项目。
答案 0 :(得分:1)
我会尝试在项目模型上创建2个属性,&#39; show&#39;和&#39; toggleTextArea&#39;:
<!-- ko foreach: projects -->
<div id="eachOppyProject" style="border-bottom: 1px solid #eee;">
<table>
<tbody>
<tr>
<td><a data-bind="attr: { href: '/tools/oppy/' + guid }" style="font-size: 25px;"><span class="link" data-bind=" value: guid, text: name"></span></a></td>
</tr>
<tr data-bind="text: projectDescription"></tr>
<tr data-bind="text: guid"></tr>
</tbody>
</table>
<span class="forminputtitle">Have you done project this before?</span>
<input type="button" id="oppyBtn" class="btnOppy" value="Yes" data-bind="click: toggleTextArea" />
<textarea id="oppyDoneTextArea" placeholder="Tell us a little of what you've done." style="height:75px;" data-bind="visible: show" /><br />
</div>
<!-- /ko -->
您的项目模型可能是这样的:
var project = function(){
var self = this;
self.show = ko.observable(false);
self.toggleTextArea= function(){
self.show(!self.show());
};
};
这允许单击按钮切换textarea的状态。
答案 1 :(得分:-1)
我想你可以试试这个来获得结果。
HTML(按钮和textarea是最后两个控件):
<!-- ko foreach: projects -->
<div id="eachOppyProject" style="border-bottom: 1px solid #eee;">
<table>
<tbody>
<tr>
<td><a data-bind="attr: { href: '/tools/oppy/' + guid }" style="font-size: 25px;"><span class="link" data-bind=" value: guid, text: name"></span></a></td>
</tr>
<tr data-bind="text: projectDescription"></tr>
<tr data-bind="text: guid"></tr>
</tbody>
</table>
<span class="forminputtitle">Have you done project this before?</span>
<input type="button" id="oppyBtn" class="btnOppy" onclick="displayTextArea(this)" value="Yes" />
<textarea id="oppyDoneTextArea" placeholder="Tell us a little of what you've done." style=" display:none; height:75px; " /><br />
</div>
<!-- /ko -->
JavaScript的:
function displayTextArea(element) {
var my_disply = element.nextSibling
if (my_disply == "block")
document.getElementById('oppyDoneTextArea').style.display = "none";
else
document.getElementById('oppyDoneTextArea').style.display = "block";
}
希望它会对你有所帮助,并且只有当textarea是下一个兄弟按钮点击你必须显示textarea时才会工作