【问题标题】:How to use checkbox implement different functions at same time JavaScript如何使用复选框同时实现不同的功能 JavaScript
【发布时间】:2017-05-13 12:34:11
【问题描述】:

我对 JavaScript 很陌生,对 JavaScript、JQuery、Vanilla JavaScript 的语言有点困惑。我在一个 html 中有两个复选框,每个复选框都有两个不同的功能。我应该怎么做才能纠正这个功能?

HTML 代码:

<div class = "multianswer">
  <input type = "checkbox" name = "answercheck" id= "answercheck">
  <label for = "answercheck"> Show Answers </label>
</div>


<div class = "multifeature">
  <input type="checkbox"  name="featurescheck" id="featurescheck">
  <label for = "featurescheck"> Show More Features </label>
</div>

JavaScript 代码:

    $("input:checkbox").click(function(){
            $("input:checkbox:checked").click(function(){ 

//I think is some problem about this ***(function), because if I put 
// ".each(function)", all the checkbox and the function is working. And I 
//  have tried use checkbox id to identify each checkbox, but it is not 
//  working, don't understand why? 

                showAnswer()  //This is the one of the functions
            })
        })

    $("input:checkbox").click(function(){
            $("input:checkbox:checked").click(function(){  //same as above
                showMoreFeatures()  //This is another function
            })
        })

考虑我想要的最终效果,我认为有两个关键问题: 1. 如代码所示,如​​何使每个复选框发挥不同的功能。 2.这两个功能会在一个文本区域实现不同的效果,我希望这两个效果可以一起显示,不要相互覆盖。所以这就是我使用复选框的原因,但如何实现我仍然没有'不知道。

谢谢你的帮助~

补充信息:这是两个功能,但是我检查了之后,如果我想同时显示两个效果,我仍然认为问题不在这里

        function showAnswer(){
            var answerWords=$(".final .answer p").text();
            var text= $(".article p").text(); 
            var txt1 = answerWords;
            var txt2 = '<span style = "background-color:yellow">' + txt1 + '</span >'
            var reg = new RegExp(txt1,"g")
            text = text.replace(reg ,txt2)

            $(".article p").html(text) 

        };

        function showMoreFeatures(){
            var text= $(".article p").text(); 
            var qus = item2.question;
            var newCheckQus = new Array();
            var subQus= qus.substr(0,qus.length-1);
            var checkQus = subQus.split(" ");

            for(var i =0; i < checkQus.length; i++){
                if(except.indexOf(checkQus[i]) >-1){
                    continue    
                }else{
                    var txt2 = '<span style = "background-color:#2ECCFA">' + checkQus[i] + '</span >'
                    var reg = new RegExp(checkQus[i] ,"g")
                    text = text.replace(reg ,txt2)
                }   
                }

                $(".article p").html(text)
        }

【问题讨论】:

    标签: javascript jquery html checkbox


    【解决方案1】:

    您可以使用以下道具检查复选框是否选中 - 如果为 true,则执行代码

    $("#featurescheck").change(function() {
        if($(this).prop('checked') == true) {
            //your code here;
        } else {
            //your code here
        }
    });
    $("#answeredcheck").change(function() {
        if($(this).prop('checked') == true) {
            //your code here;
        } else {
            //your code here
        }
    });

    【讨论】:

    • 非常感谢,可以了,但是还有一个问题,就是问题的重点2,两个函数的效果只是相互覆盖,一个实现了,又检查了一个,第一个效果就消失了。如何解决这个问题?谢谢,否则仅设置复选框就足够了吗?我应该改进我的功能吗?我的两个功能是为文本中的不同单词替换不同的颜色,其中一些会得到黄色背景,其中一些会得到另一种颜色背景,最后我需要显示所有颜色。
    【解决方案2】:

    您可以在一个事件中检查或取消选中复选框。使用单击事件而不是更改事件。

    更新了Fiddle

    $(document).ready(function(){
    $("input:checkbox").click(function() {
        if($(this).attr("id") === "answercheck"){
         if( $(this).attr("checked")){
          alert('answercheck checked');
        }
        else{
          alert('answercheck unchecked');
        }
        }
       if($(this).attr("id") === "featurescheck" ){
        if( $(this).attr("checked")){
          alert('featurescheck checked');
        }
        else{
          alert('featurescheck unchecked');
        }
        }
       });
    });
    

    【讨论】:

    • 对不起,按照这种方式,它不起作用,甚至调用该函数也不起作用。但如果我将“attr”更改为“prop”,它将调用该函数。具有点击功能。但是最终效果还是被覆盖了,只能在颜色上显示(一个功能效果,不是两个)
    • 非常感谢更新,但问题仍然是它们被覆盖了。例如,显示答案功能会得到一些单词黄色,显示功能功能会得到一些单词蓝色,所有这些单词都在一个文本区域中,最终效果,在我选中所有这两个复选框后,应该在这个文本区域中显示黄色和蓝色的词。但是现在,我检查它们时只能得到蓝色或黄色,取决于哪个是最后一个,然后颜色将保持不变。
    猜你喜欢
    • 1970-01-01
    • 2016-01-31
    • 2012-06-16
    • 2020-04-07
    • 2019-02-25
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多