简体   繁体   English

在按钮内运行代码而无需单击它

[英]Run code inside a button without clicking it

the following part of code is from a pop-up alert window, in the example page on how to use there is a button that says "Error", and when you click it the window appears. 以下代码部分来自一个弹出警报窗口,在示例页面中有关如何使用的示例中,有一个显示“错误”的按钮,当您单击它时,将显示该窗口。

<button class="btn btn-danger notification" data-verti-pos="bottom" data-horiz- pos="right" data-message="<i class='fa fa-frown-o' style='padding-right:6px'></i> Oops... Something went wrong. Hope we don\'t see me again." data-type="error" type="button"></button>

The problem is that i want to call the window without needing to click the button, i am trying to call it inside this if conditional. 问题是我想在不单击按钮的情况下调用该窗口,如果有条件,我想在此窗口中调用它。

if(codigo=="matricula_longitud"){
        //I know the part of code below is totally wrong
        document.getElementById("errores").innerHTML='<div class="alert alert-warning fade in"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><strong>Warning!</strong> Better check yourself, youre not looking too good.</div>';
    return "si";
}

I am new to programming so any advice will be appreciated. 我是编程的新手,所以任何建议都将不胜感激。 Thank you. 谢谢。

You can wrap the content in a function, which is called from the event handler of your button and other function where you're showing the window without the requirement to click on a button. 您可以将内容包装到一个函数中,该函数可以从按钮的事件处理程序以及显示窗口的其他函数中调用,而无需单击按钮。

var showWindow = function() {
    alert("La matricula es muy corta")
    //I know the part of code below is totally wrong
    document.getElementById("errores").innerHTML='...';
    return "si";
};

And then in your button handler 然后在您的按钮处理程序中

if(codigo=="matricula_longitud"){ 
    return showWindow();
}

And in your other function, just call showWindow() if you want to show it to the user. 在其他函数中,如果要显示给用户,只需调用showWindow()即可。

A little remark though, if you want to check a condition, it's best to use strict equality checks, === or !== instead of == or != . 不过,请注意,如果要检查条件,最好使用严格的相等性检查, ===!==而不是==!= Please refer to this question for further explanation. 请参考此问题以进一步解释。

At least, you can use jquery 至少可以使用jquery

<button id="myBtn" class="btn btn-danger notification" data-verti-pos="bottom" data-horiz- pos="right" data-message="<i class='fa fa-frown-o' style='padding-right:6px'></i> Oops... Something went wrong. Hope we don\'t see me again." data-type="error" type="button"></button>

$('#myBtn').click();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM