是否存在类似jQuery.toggle(boolean)的东西? [英] Does something like jQuery.toggle(boolean) exist?

查看:256
本文介绍了是否存在类似jQuery.toggle(boolean)的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了很多类似于以下代码的东西。它基本上根据某些条件切换元素。

I write something similar to the following code a lot. It basically toggles an element based on some condition.

在下面的组成示例中,条件是如果同意复选框,名称字段不为空。

In the following made-up example, the condition is "If the agree checkbox is checked and the name field isn't empty".

$("button").click(function() {
  if ($("#agree").is(":checked") && $("#name").val() != "" ) {
    $("#mydiv").show();
  } else {
    $("#mydiv").hide();
  }
});

我希望有某种jQuery函数可以这样工作。

I wish there was some sort of jQuery function that would work like this.

$("button").click(function() {
  var condition = $("#agree").is(":checked") && $("#name").val() != "" );
  $("#mydiv").toggle(condition);
});

那里有类似的东西吗?或者除了第一个例子之外还有其他方法以较少的 if-else-ish 的方式执行此操作?

Is there something like this out there? Or are there other ways besides the first example to do this in a less if-else-ish way?

推荐答案

好的,所以我是个白痴,在我提问之前需要RTM。

Ok, so I am an idiot and need to RTM before I ask questions.

jQuery.toggle()允许您开箱即用。

$("button").click(function() {
  var condition = $("#agree").is(":checked") && $("#name").val() != "" );
  $("#mydiv").toggle(condition);
});

这篇关于是否存在类似jQuery.toggle(boolean)的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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