-4

我正在尝试使用复选框进行文本搜索。例如,如果该人选中该复选框,它将显示用户在搜索框中输入的单词/字母(该单词/字母将被突出显示)。假设我输入“the”,它将搜索段落中的所有“the”并突出显示所有“the”。我已经了解了第一部分,我不明白的是如何使复选框与文本搜索表单连接。因此,当用户选择复选框时,将显示“the”或他们在搜索框中输入的任何单词/字母。

我正在考虑使用 if 语句...

4

2 回答 2

1

因此,如果您想与该复选框进行交互,您可以执行以下操作:

$(':checkbox').on('change', function() { 
    if ($(this).is(':checked')) { 
        // do your search thing 
    } else {
        // turn off your search thingy
    } 
});

小提琴

于 2015-06-26T21:44:26.810 回答
0

你可以使用这样的东西:

$(':checkbox').on('change', function() {
    if ($(this).is(':checked')) {
        $(".content").addClass("highlight");
    } else {
        $(".content").removeClass("highlight");
    }
});

在 CSS 中,您需要:

.highlight {background: #99f;}

片段

$(function () {
  text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Incidunt repellat sint eligendi adipisci consequuntur perspiciatis voluptate sunt id, unde aspernatur dolor impedit iure quaerat possimus nihil laboriosam, neque, accusamus ad.";
  $(".content").text(text);
  $(':checkbox').on('change', function() {
    if ($(this).is(':checked')) {
      $(".content").addClass("highlight");
      $(".content").html(text.replace(/lo/gi, '<span>lo</span>'));
    } else {
      $(".content").removeClass("highlight");
    }
  });
});
.check + input {display: none;}
.check:checked + input {display: inline-block;}
.highlight span {background: #ccf;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="checkbox" class="check" />
<input type="text" placeholder="Type your terms..." class="term" />
<div class="content"></div>

也许像上面这样。

于 2015-06-26T21:45:07.097 回答