简体   繁体   English

为什么必须两次单击此输入按钮才能调用函数?

[英]Why do I have to click this input button twice to call a function?

I have a simple input button (not a submit) with an onclick function to show or hide another div. 我有一个简单的输入按钮(不是提交),带有onclick函数来显示或隐藏另一个div。 For some reason, I have to click the button twice, once to select it, then a second time to execute the function. 由于某种原因,我必须单击两次按钮,一次选择它,然后第二次执行该功能。

This is the short version: 这是简短的版本:

<input type="button" id="request_info_layer_button" onclick="showForm()" value="REQUEST INFORMATION" />

function showForm() {
var isdisplayed = document.getElementById("request_info_form_layer_wrapper").style.display;
if (isdisplayed == "none") {
    document.getElementById("request_info_form_layer_wrapper").style.display = "block";
    document.getElementById("request_info_layer_button").style.borderBottom = "0";
    document.getElementById("request_info_layer_button").style.borderLeft = "0";
    document.getElementById("request_info_layer_button").style.borderTop = "solid 3px #C4591B";
    document.getElementById("request_info_layer_button").style.borderRight = "solid 4px #C4591B";
} else {
    document.getElementById("request_info_form_layer_wrapper").style.display = "none";
    document.getElementById("request_info_layer_button").style.borderTop = "0";
    document.getElementById("request_info_layer_button").style.borderRight = "0";
    document.getElementById("request_info_layer_button").style.borderLeft = "solid 3px #DFBC81";
    document.getElementById("request_info_layer_button").style.borderBottom = "solid 4px #DFBC81";
}

} }

The full code and button behavior is here: http://jsfiddle.net/yp5an1w7/3/ 完整的代码和按钮行为在此处: http : //jsfiddle.net/yp5an1w7/3/

The first time, isdisplayed is empty, so you jump to the else condition. 第一次,isdisplayed为空,因此您跳到else条件。 After the else is done, the style is set to none. 完成else后,将样式设置为none。

The second call, sees the style as none, and updates it to block, then displays it. 第二个调用将样式视为无,并将其更新为阻止,然后显示。

As mentioned below, add the display:none directly on the id to solve the issue 如下所述,在id上直接添加display:none即可解决问题

<div id="request_info_form_layer_wrapper" style="display:none;">

When you click the first time, the onclick event is fired and it goes to the else part, because your style attribute is not set and hence display gives you empty value which is not equal to 'none'. 第一次单击时,将触发onclick事件,然后转到else部分,因为未设置您的style属性,因此显示为您提供了不等于“ none”的空值。 Hence it goes to the else part and assigns the display attribute as 'none'. 因此,它转到其他部分,并将显示属性分配为“无”。 so 所以

<div id="request_info_form_layer_wrapper" style="display:none">
<div id="request_info_form_wrapper">
    <form name="request_info" id="request_info_form" action="/somepage" method="post">
        <h3>Name</h3>
        <input type="text" id="name_input" /><br />
        <p>Preferred method of contact</p>
        <h3>Email</h3>
        <input type="text" id="email_input" /><br /><br />
        <h3>Phone</h3>
        <input type="text" id="phone_input" /><br /><br />
        <p>Thanks for your interest in our program. We'll only use your contact information to send additional materials to help you understand the program better.</p>
        <input type="submit" id="submit_button" value="SUBMIT" />
    </form>
</div>

Put the above and your function should work on the first click. 放在上面,您的功能应该在第一次单击时起作用。

Checking if the display style is none will fail. 检查显示样式是否为无。 You can fix it by changing your css or by swapping your function around to check if isdisplayed == 'block' rather than isdisplayed == 'none'. 您可以通过更改css或交换函数来检查isdisplay =='block'而不是isdisplayed =='none'来修复它。

There's a good explanation here: Why does my button need to be clicked twice for the event handler to work the first time, but thereafter just once? 这里有一个很好的解释: 为什么我的按钮需要两次单击才能使事件处理程序第一次起作用,而之后才需要单击一次?

There is a difference between the element's style attribute and the class property. 元素的样式属性和类属性之间存在区别。 Meaning yourElement.style is the attribute attached to the element. 意味着yourElement.style是附加到元素的属性。 This is not the same as the styles defined by a css class. 这与css类定义的样式不同。 You can either add a style attribute to your element: 您可以在元素中添加样式属性:

<div style="display:none;">...</div>

or access the underlying css class: 或访问基础的CSS类:

// pure JS
document.styleSheets[0].cssRules[0]
// but I would highly recommend to use jQuery:
$("#element").css("style", "none");
// and check via 
var isDisplayed = $("#element").css("style");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我必须单击按钮两次才能加载图像? - Why do I have to click the button twice to have the image loaded? 为什么我必须在事件触发之前单击两次按钮? - Why do I have to click button twice before event fires? 为什么我必须点击两次才能让这个 JavaScript function 起作用? - Why do I have to click twice for this JavaScript function to work? 为什么必须单击两次才能使按钮起作用,如何解决此问题? - Why do I have to click twice for the button to work and how can I fix this? 为什么我必须单击提交按钮 TWICE 才能更新我的 useState() 挂钩? - Why do I have to click Submit button TWICE in order to update my useState() hook? Firebase:我必须单击“继续”按钮两次才能执行任何操作 - Firebase: I have to click 'Continue' button twice to do anything JavaScript:为什么必须在表中单击两次才能执行colorActiveLine函数? - JavaScript : Why do i have to click twice in the table to execute the colorActiveLine function? 为什么我必须单击两次才能触发事件? - Why do I have to click twice for an event to fire in react? EventListener 和 onClick - 为什么我必须点击两次才能触发? - EventListener and onClick - why do I have to click twice to trigger? 为什么我必须第一次点击 Div 两次? - Why Do I Have To Click the Div Twice the 1st Time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM