[英]Javascript working on second button click instead of first
我有一个试图在单击按钮时显示和激活表单信息的表。 该表设置为“显示:无”,并且按钮已连接到javascript。 javascript可以正常运行,但仅在第二次单击按钮后才能运行。 我希望它在第一次单击时能工作。
按钮和表格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>
这是表CSS:
#newPatientForm {
display: none;
}
这是按钮调用的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);
}
},
在样式表中设置计算样式后,您需要检查
function isHidden(elem) {
var style = window.getComputedStyle(elem);
return style.display === "none";
}
更改
if (divElement.style.display == 'none') {
至
if (isHidden(divElement)) {
使用上面的方法。
最好只拥有一个隐藏或未隐藏的类,然后切换该类。
最后,我不认为self.Patients.pop(patient);
正在做您认为正在做的事情。 pop
方法没有参数。 它不会查找和删除元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.