jQuery - 使用复选框切换文本

时间:2015-07-07 04:28:59

标签: jquery jquery-ui

我一直在使用jQuery代码(来源:http://jqueryui.com/button/#checkbox)并尝试添加切换功能,因此在点击时,某些文字会隐藏/取消隐藏。

我在这方面有点先发制人,所以不确定我是否朝着正确的方向前进。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Button - Checkboxes</title>
  <link rel="stylesheet"     href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#check" ).button();
    $( "#format" ).buttonset();
  });
  </script>

<script>
$( "#check1" ).click(function() {
  $( "#text1" ).toggle();
});
</script>

  <style>
  #format { margin-top: 2em; }
  </style>
</head>
<body>
<div id="format">
  <input type="checkbox" id="check1"><label for="check1">Option 1</label>
  <input type="checkbox" id="check2"><label for="check2">Option 2</label>
  <input type="checkbox" id="check3"><label for="check3">Option 3</label>
</div>
</body>
</html>
<p id="text1">Hello</p>
<p id="text2">Bye</p>
<p id="text3">Other</p

所以我试图在这里添加.toggle部分,但我无法做任何事情。这可能是由于我试图识别事物的方式 - 如果是这样,我怎么能这样才能使它工作?

我必须在工作时添加更多切换条件,以便: 选择选项1时,将显示text1,text2,text3 选项2仅选择文本1,文本2显示 选项3仅选择文本2,显示文本3.

如果需要澄清,请告诉我。对此的任何帮助将不胜感激。

-Thanks

3 个答案:

答案 0 :(得分:1)

简短的甜蜜代码。

boolean isTextNotEmpty=validate();
if(isTextNotEmpty){
btnRegistrarse.setEnabled(true);
}

<强> Demo

答案 1 :(得分:0)

我会直接使用代码段解决您的问题,但您的代码中存在一些需要先修复的结构问题。事实上你已经有了代码,你只需稍微改变一下文档的结构。

  • 首先,您的<p>元素必须位于文档的<body>标记内。您当前拥有它们的方式(在<html>元素之外)是无效的HTML。

  • 其次,您的javascript需要位于您的身体底部。一旦浏览器遇到Javascript就会执行,因此当前编写代码的方式,您正在尝试使用jQuery来选择尚未创建的元素。

以下是您的代码的修订版:

<!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>jQuery UI Button - Checkboxes</title>
      <link rel="stylesheet"     href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
      <link rel="stylesheet" href="/resources/demos/style.css">


      <style>
      #format { margin-top: 2em; }
      </style>
    </head>
    <body>
    <div id="format">
      <input type="checkbox" id="check1"><label for="check1">Option 1</label>
      <input type="checkbox" id="check2"><label for="check2">Option 2</label>
      <input type="checkbox" id="check3"><label for="check3">Option 3</label>
      <p id="text1">Hello</p>
      <p id="text2">Bye</p>
      <p id="text3">Other</p>
      <script>
      $(function() {
          $( "#check" ).button();
          $( "#format" ).buttonset();
      });
      $( "#check1" ).click(function() {
          $( "#text1" ).toggle();
      });
      </script>
    </div>
    </body>
    </html>

答案 2 :(得分:0)

更新您的代码,如下所示

DEMO

[
    {
    "T": "Test",
    "About": "About T"
    }
]

的jQuery

<div id="format">
  <input type="checkbox" id="check1" data-id="text1"><label for="check1">Option 1</label>
  <input type="checkbox" id="check2" data-id="text2"><label for="check2">Option 2</label>
  <input type="checkbox" id="check3" data-id="text3"><label for="check3">Option 3</label>
</div>

<p id="text1">Hello</p>
<p id="text2">Bye</p>
<p id="text3">Other</p>

使用data属性引用文本元素id标记id。