如何验证具有多个值的复选框

时间:2022-12-02 17:37:28

I have tried all i can to get a solution by googling, but couldnt get one. i have the following php code which outputs a checkbox with multiple values, the values are from a database.

我已尽力通过谷歌搜索获得解决方案,但无法得到一个。我有以下PHP代码输出一个具有多个值的复选框,值来自数据库。

echo  '<input type="checkbox" name="courses[]" value="'.$courses["id_course"].'" course-credit="'.$courses["course_credit"].'" onclick="return course_credit();"/>  '.$courses["course_name"].';

when the check boxes are clicked, the javascript code inside course_credit() should run and adds up the course-credit values. i know how to go after that. but i am not sure how i can get the course-credit values.

单击复选框时,应运行course_credit()中的javascript代码并将课程学分值相加。我知道该怎么做。但我不确定如何才能获得课程学分。

1 个解决方案

#1


0  

Edit based on comments below... To make it work only on "ckeck" (not "uncheck").

根据以下评论进行编辑...使其仅适用于“ckeck”(而非“取消选中”)。

This should work:

这应该工作:

onclick="course_credit(this);"

In your function course_credit(), you'll get the value between the parenthesis. So do something like:

在函数course_credit()中,您将获得括号之间的值。所以做以下事情:

function course_credit(obj){
    value = obj.value;

    if(obj.checked){
        credit = value*2;   // Seen in comment that you wanted it to double.
    }else{
        credit = 0;         // I dont know here... Set a default value for this case.
    }
    alert(value);
    // Rest of your code.
}

#1


0  

Edit based on comments below... To make it work only on "ckeck" (not "uncheck").

根据以下评论进行编辑...使其仅适用于“ckeck”(而非“取消选中”)。

This should work:

这应该工作:

onclick="course_credit(this);"

In your function course_credit(), you'll get the value between the parenthesis. So do something like:

在函数course_credit()中,您将获得括号之间的值。所以做以下事情:

function course_credit(obj){
    value = obj.value;

    if(obj.checked){
        credit = value*2;   // Seen in comment that you wanted it to double.
    }else{
        credit = 0;         // I dont know here... Set a default value for this case.
    }
    alert(value);
    // Rest of your code.
}