简体   繁体   English

更改复选框+标签jQuery

[英]Change checkbox + label jquery

I got multiple checkboxes and labels and when the answer is correct I want to change the background color of the selected checkbox and label 我有多个复选框和标签 ,当答案正确时,我想更改所选复选框和标签背景色

<input type="checkbox" id="a" class="check-with-label" />
<label for="a" class="question_box">
    <?php echo $vraag[0]['A'] ?>
</label>

<input type="checkbox" id="b" class="check-with-label" />
<label for="b" class="question_box">
    <?php echo $vraag[0]['B'] ?>
</label>

and when the user presses submit it calls an ajax call to a script to check if it is the correct answer or not. 当用户按下Submit时,它会向脚本调用ajax调用 ,以检查其答案是否正确。

$('#bottom').on('click', function(){
    $('#antwoorden input:checked').each(function() {
        var selected = $(this).attr('id');
        //var selected_box = this;
        selected = selected.toUpperCase();

        var data = {
            vraag_nummer : <?php echo $vraag[0]['id'] ?>,
            antwoord     : selected
        };

        console.log(data);
        $.ajax({
            type: 'POST',
            url : 'includes/check_correct.php',
            data: data,
            success: function(data) {
                if(data.correct){
                    this.css('background-color', 'green');
                    //selected_box.css('background-color', 'green');
                } else {
                    this.css('background-color', 'red');
                }
            },
            error: function(err) {
                console.log('error');
                console.log(err);
            }
        });
    });
});

problem is I don't know how to call the correct box because when the user presses submit the this variable is now the submit button and not the checked box (There can only be one checkbox selected at the time_. 问题是我不知道如何来调用正确的框,因为当用户按下提交 this变量是现在提交按钮,而不是选中复选框(只能有在time_选择一个复选框。

So the question is how do I replace the background color of the selected AND the correct box from 所以问题是我该如何替换所选的背景色和正确的框

.check-with-label:checked + .question_box {
    background-color: orange;
} 

to

background-color:green

(and for incorrect red ) (以及不正确的red

Try changing the label instead of this 尝试更改label而不是this label

You've already got the ID of the label 您已经有标签的ID

( var selected = $(this).attr('id'); ) var selected = $(this).attr('id');

Which will probably return a/b/c/d and then use 可能会返回a / b / c / d然后使用

$('label[for="'+selected+'"]').css('background-color', 'green'); //or red in the else statement

this styles the label for the selected id, in this case d the code above basicly means: 将为所选ID标签设置样式,在这种情况下,d上面的代码基本上意味着:

$('label][for="d"].css('background-color','green'); //or red

but then using the chosen variable 但是然后使用选择的变量

Goodluck! 祝好运!

In success use selected_obj.css instead of this.selected and pass $(this) to selected_obj 成功使用selected_obj.css代替this.selected并将$(this)传递给selected_obj

$('#bottom').on('click', function(){
    $('#antwoorden input:checked').each(function() {
        var selected = $(this).attr('id');
        var selected_obj = $(this);
        //var selected_box = this;
        selected = selected.toUpperCase();

        var data = {
            vraag_nummer : <?php echo $vraag[0]['id'] ?>,
            antwoord     : selected
        };

        console.log(data);
        $.ajax({
            type: 'POST',
            url : 'includes/check_correct.php',
            data: data,
            success: function(data) {
                if(data.correct){
                    selected_obj.css('background-color', 'green');
                    //selected_box.css('background-color', 'green');
                } else {
                    selected_obj.css('background-color', 'red');
                }
            },
            error: function(err) {
                console.log('error');
                console.log(err);
            }
        });
    });
});

Try this 尝试这个

$('#bottom').on('click', function(){
    $('#antwoorden .check-with-label').each(function() {
        var selected = $(this).attr('id');
        var isChecked = $(this).is(':checked'));
        if (!isChecked) {
            return;
        }
        var selected_obj = $(this);
        //var selected_box = this;
        selected = selected.toUpperCase();

        var data = {
            vraag_nummer : <?php echo $vraag[0]['id'] ?>,
            antwoord     : selected
        };

        console.log(data);
        $.ajax({
            type: 'POST',
            url : 'includes/check_correct.php',
            data: data,
            success: function(data) {
                if(data.correct){
                    selected_obj.css('background-color', 'green');
                } else {
                    selected_obj.css('background-color', 'red');
                }
            },
            error: function(err) {
                console.log('error');
                console.log(err);
            }
        });
    });
});

After the page is rendered and displayed to user, PHP will no longer run. 页面呈现并显示给用户后,PHP将不再运行。 Therefore, in your $('#bottom').on('click' function, the PHP code won't work. The trick is to store that information when building the page, such as by writing it into a data attribute or some such. 因此,在您的$('#bottom').on('click'函数中,PHP代码将无法正常工作。诀窍是在构建页面时存储该信息,例如将其写入data属性或其他这样。

Inside the AJAX success function the $(this) object is not available. 在AJAX成功函数中, $(this)对象不可用。 Set it to a global variable and you will be able to access the value. 将其设置为全局变量,您将可以访问该值。

 /* javascript/jQuery */ $('#bottom').on('click', function(){ $('#antwoorden input:checked').each(function() { var selected = this.id; //faster, same result $this = $(this); selected = selected.toUpperCase(); var data = { //change next line - PHP finished running vraag_nummer : $this.attr('data-vn'), antwoord : selected }; console.log(data); $.ajax({ type: 'POST', url : 'includes/check_correct.php', data: data, success: function(data) { if(data.correct){ $this.css('background-color', 'green'); } else { $this.css('background-color', 'red'); } }, error: function(err) { console.log('error'); console.log(err); } }); }); }); 
 <!-- HTML: --> <input id="vraag_nummer" data-vn="<?php echo $vraag[0]['id'] ?>" type="text" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM