简体   繁体   English

Javascript在第二个按钮上单击而不是第一个按钮

[英]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.

相关问题 按钮在第二次单击而不是 Reactjs 中的第一次单击时工作 - Button working on second click instead of first in Reactjs Javascript 按钮切换在第一次点击时不起作用,只有第二次点击或更多 - Javascript button toggle not working on first click, only second click or more 单击时播放Javascript声音,第二个按钮单击停止第一个声音 - Javascript play sound on click, second button click stop first sound 如何通过点击第一个按钮将第二个按钮传递给javascript函数 - how to pass second button on click over the first button to a javascript function 在javascript中的第二个按钮的确认上调用第一个按钮不起作用 - Calling first button on confirm of second button in javascript isn't working 按钮在第一次点击时没有使用任何过滤器,第二次它正在工作 - button is not taking any filter in first click, in second it is working 切换 - Jquery 我想每当点击它显示的第一个按钮然后点击第二个按钮然后而不是第一个按钮的 div 标签隐藏 - toggle - Jquery I want to whenever click on first button it show and then click on second button then instead first button's div tag hide 点击按钮将第二次点击数据发送到 db 而不是第一个 php/ajax - click button sends second click data to db instead of the first one php/ajax 单击第一个操作,单击第二个按钮 - Button first action on click and second action on click 第二次单击按钮不起作用(JQuery) - Second click on button is not working (JQuery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM