jQuery切换状态 [英] jQuery Toggle State

查看:125
本文介绍了jQuery切换状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题的快速而又枯燥的:

Here's the quick and skinny of my issue:

$("a").toggle(function() { /*function A*/ }, function() { /*function B*/ });

function A内部,将显示一个表单.如果用户成功完成了表单,则表单将再次隐藏(返回到其原始状态).

Inside function A a form is displayed. If the user successfully completes the form, the form is hidden again (returning to it's original state).

function B内部,相同的表单被隐藏.

Inside function B the same form is hidden.

其背后的理论是,用户可以选择显示表单并将其填写,也可以再次单击以使表单重新隐藏.

The theory behind this is that the user can choose to display the form and fill it out, or they can click again and have the form go back into hiding.

现在我的问题是这样的:当前,如果用户成功填写表格-并且它隐藏了,则用户必须点击链接 两次 ,然后返回显示表单的切换状态.

Now my question is this: currently, if the user fills out the form successfully--and it goes into hiding--the user would have to click on the link twice before returning to the toggle state that displays the form.

是否可以通过编程将拨动开关重置为其初始状态?

Is there anyway to programmatically reset the toggle switch to its initial state?

推荐答案

jQuery有两个.toggle()方法:

jQuery has two .toggle() methods:

.toggle()

切换匹配的每个集合 元素.如果显示它们,请切换 使它们隐藏.如果它们是隐藏的, 拨动使其显示出来.

Toggles each of the set of matched elements. If they are shown, toggle makes them hidden. If they are hidden, toggle makes them shown.

.toggle(even, odd)

每单击两次,即可在两个函数调用之间切换.

Toggle between two function calls every other click.

在这种情况下,您需要第一个.这样的事情应该可以解决问题:

In this case you want the first one. Something like this should do the trick:

$("a").click(function() {
    $("#theForm").toggle();
});

这篇关于jQuery切换状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆