[英]Javascript working on second button click instead of first
I have a table that I am trying to display and activate form information in on a button click. 我有一个试图在单击按钮时显示和激活表单信息的表。 The table is set to "display: none" and the button is connected to the javascript.
该表设置为“显示:无”,并且按钮已连接到javascript。 The javascript works but only after the button has been clicked a second time.
javascript可以正常运行,但仅在第二次单击按钮后才能运行。 I would like for it to work the first time it is clicked.
我希望它在第一次单击时能工作。
Button & Table HTML: 按钮和表格HTML:
<button data-bind="click: addPatient" style="margin-top: 20px; margin-bottom: 10px;">Add Patient</button>
<table id="newPatientForm">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th></th>
</tr>
<tbody data-bind="foreach:Patients">
<tr>
<td class="form-group"><input data-bind="value: FirstName, event: {change: flagPatientAsEdited}, hasfocus: true" /></td>
<td class="form-group"><input data-bind="value: LastName" /></td>
<td class="form-group"><button data-bind="click: $parent.removePatient">Delete</button></td>
</tr>
</tbody>
</table>
This is the table CSS: 这是表CSS:
#newPatientForm {
display: none;
}
This is the javascript that the button calls. 这是按钮调用的javascript。
self.addPatient = function () {
var divElement = document.getElementById('newPatientForm');
var patient = new PatientViewModel({ SiteId: 0, FirstName: "", LastName: "", ObjectState: ObjectState.Added });
if (divElement.style.display == 'none') {
divElement.style.display = 'block';
self.Patients.push(patient);
}
else {
divElement.style.display = 'none';
self.Patients.pop(patient);
}
},
You need to check the computed style when it is set in a stylesheet 在样式表中设置计算样式后,您需要检查
function isHidden(elem) {
var style = window.getComputedStyle(elem);
return style.display === "none";
}
Change 更改
if (divElement.style.display == 'none') {
to 至
if (isHidden(divElement)) {
which uses the above method. 使用上面的方法。
It would probably bet better to just have a class that is hidden or not and toggle the class. 最好只拥有一个隐藏或未隐藏的类,然后切换该类。
And finally, I do not think self.Patients.pop(patient);
最后,我不认为
self.Patients.pop(patient);
is doing what you think it is doing. 正在做您认为正在做的事情。 the
pop
method has no arguments. pop
方法没有参数。 It does not find and remove elements. 它不会查找和删除元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.