简体   繁体   English

从按下按钮中获取要持续的操作

[英]Get an action to persist from a button press

I'm using HTML + CSS + jQuery to develop a webpage, and one of the elements of the page is for the user to enter a name into a form, click submit, and then a table should pop up.我正在使用HTML + CSS + jQuery 开发一个网页,页面的元素之一是让用户在表单中输入名称,单击提交,然后应弹出一个表格。 Here is the relevant code for that part, with the submit button having class name "submit-button":这是该部分的相关代码,提交按钮的类名称为“submit-button”:

$(document).ready(main)

function main()
{
    $(".submit-button").on("click", () =>
    {
        //code to draw table
    });

    //other code...
}

The problem I'm running into is that the table flashes on screen for just the length of the button click (which is basically nothing) and then disappears.我遇到的问题是表格在屏幕上闪烁的时间只有按钮点击的长度(基本上没有),然后消失。 My question is, how do I get this action to persist outside of the button press?我的问题是,如何让这个动作在按下按钮之外持续存在? It needs to be triggered by the button press but also needs to last outside of it.它需要通过按下按钮来触发,但也需要在按钮之外持续。 I know the table drawing code works when run outside of the event handler, but I want to make it so the table only shows up when the button is pressed.我知道表格绘制代码在事件处理程序之外运行时可以工作,但我想让它只在按下按钮时显示表格。 Any ideas?有任何想法吗?

Considering the table to be inside a div with id "mytable", use:考虑到该表位于 ID 为“mytable”的 div 内,请使用:

$(".submit-button").on("click", () =>
    {
        document.getElementById("mytable").style.display="block";
    });

Remember to keep the style of "mytable" as display:none initially.请记住最初将“mytable”的样式保持为display:none Hopefully this is what you need.希望这就是你所需要的。

Continuing with what Arex said, I have used .css() method.继续Arex所说的,我使用了.css()方法。

In the CSS I have set table { display: hidden; }在 CSS 中我设置了table { display: hidden; } table { display: hidden; }

Alternatively, you can use $('table').hide() or $('table').show() .或者,您可以使用$('table').hide()$('table').show()


 $(document).ready(() => { $('table') $('button').on('click', () => { $('table').css('display', 'block'); }); });
 table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; display: none; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table> <br> <button>Show Table</button>

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

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