简体   繁体   English

如何在每个查询结果中添加复选框?

[英]How to add checkbox to each query result?

I have a problem with this code. 我对此代码有疑问。 I'm listing a couple of products on my page and each of them has a checkbox from query. 我在页面上列出了几个产品,每个产品都有一个查询复选框。 I would like to compare max two of the products with the „compare“ button. 我想用“比较”按钮比较最多两个产品。 So first, I would „check“ the two products and then click „compare“ button which would then redirect me to another page, where those two products would be compared. 因此,首先,我将“检查”这两个产品,然后单击“比较”按钮,然后将我重定向到另一页,将对这两个产品进行比较。

With this code submit button is enabled when other checkboxes are checked, but now I want limit checked checkboxes to 2. 使用此代码时,选中其他复选框时启用了提交按钮,但是现在我希望将选中的复选框限制为2。

I've tried with var=max in this function, but then submit button is disabled when checkboxes are checked. 我已经尝试在此函数中使用var = max,但是选中复选框后,提交按钮被禁用。

Can this be done in this function, or in new function? 可以在此功能或新功能中完成此操作吗?

  <?php $query = "SELECT * FROM artikli WHERE kategorija='laptop'"; $result = mysqli_query($con, $query); while ($row = mysqli_fetch_array($result)) { $id = $row ['id']; print "<div id='proizvod'></br><a style='text-decoration:none; color:black;' class='two' href='proizvod.php?id=$id' >" . $row["naziv"] . "" . "<p><img src=" . $row["slika"] . " width='200px' height='200px' style='border-radius: 15px;'></p>" . "<p> Cijena za gotovinu: " . $row["cijena"] . " KN </p>" . "<pre id='pre1'>" . $row["opis"] . "</pre>" . "</a><input type='checkbox' id='checkme_" . $id . "'/></div>"; } ?> </form> <input type='submit' name='usporedi' disabled='disabled' id='usporedi' onclick="window.location.href='usporedi'" value='Usporedi' /> <script type="text/javascript"> window.onload = function () { var form = document.getElementById('Forma'); var usporedi = document.getElementById('usporedi'); var e; for (i = 0, n = form.elements.length; i < n; i++) { e = form.elements[i]; if (e.type == 'checkbox') { if (e.id.indexOf('checkme') == 0) { e.addEventListener('change', function () { if (this.checked) { usporedi.disabled = false; } else { usporedi.disabled = true; } }); } } } }; </script> 

Try something like this: 尝试这样的事情:

Create your form elements: 创建表单元素:

<form id="myForm">
<?php

$query = "SELECT * FROM artikli WHERE kategorija='laptop'";

$result = mysqli_query($con, $query);

while ($row = mysqli_fetch_array($result)) {

    $id = $row ['id'];

    print
        "<div id='proizvod'></br><a style='text-decoration:none; color:black;' class='two' href='proizvod.php?id=$id' >" . $row["naziv"] . "" .
        "<p><img src=" . $row["slika"] . " width='200px' height='200px' style='border-radius: 15px;'></p>" .
        "<p> Cijena za gotovinu: " . $row["cijena"] . " KN </p>" .
        "<pre id='pre1'>" . $row["opis"] . "</pre>" .
        "</a>**<input type='checkbox' id='checkme_" . $id . "'/>**</div>
}
?>
</form>

Notice here that I moved your opening <form> tag to the top and gave it an id. 请注意,在这里,我将打开的<form>标记移至顶部并为其指定了ID。 I also made sure that each of your checkboxes has a unique id by adding _".$id." 通过添加_".$id." ,我还确保您的每个复选框都有唯一的_".$id." to each element id. 到每个元素ID。

Now the magic: 现在魔术:

<script type="text/javascript">

window.onload = function(){

    var form = document.getElementById('myForm');
    var usporedi = document.getElementById('usporedi');
    var e;

    for( i = 0, n = form.elements.length; i < n; i++ )
    {
        e = form.elements[i];

        if( e.type == 'checkbox' )
        {
            if( e.id.indexOf('checkme') == 0 )
            {
                e.addEventListener( 'change', function()
                {
                    if( this.checked )
                    {
                        usporedi.disabled = false;
                    } else {
                        usporedi.disabled = true;
                    }

                } );
            }
        }
    }
}

</script>

The javascript here looks through all the elements of your form, and for each checkbox that it finds, it checks that the word 'checkme' appears at the beginning of the id (we don't care what the rest of it is in this instance). 此处的javascript浏览了表单的所有元素,并针对找到的每个复选框,检查是否在id的开头出现了“ checkme”一词(在这种情况下,我们不在乎它的其余部分是什么) )。 Then it adds an event listener to each 'checkme' checkbox which will disable / enable the submit button when each checkbox is toggled. 然后,它将事件侦听器添加到每个“ checkme”复选框,当切换每个复选框时,它将禁用/启用“提交”按钮。

EDIT - ONLY 2 CHECKBOXES ACTIVE AT ONCE 编辑-一次仅激活2个复选框

OK ... make a coffee, this is going to take a while. 好吧...煮咖啡,这需要一段时间。

ADDING THE EVENT LISTENER 添加事件倾听者

There's only 1 thing we want to do automatically when the document loads - assign an event listener to each checkbox in the form with the appropriate id stub. 当文档加载时,我们只想自动执行一件事-将事件侦听器分配给表单中具有相应ID存根的每个复选框。 Note that NOTHING changes with regard to the creation of your form elements as demonstrated above. 请注意,如上所示,关于表单元素的创建没有任何变化。

window.onload = function(){

    var form = document.getElementById('myForm'), e;

    for( i = 0, n = form.elements.length; i < n; i++ )
    {
        e = form.elements[i];
        if( e.type == 'checkbox' && e.id.indexOf('checkme') == 0 )
        {
            e.addEventListener( 'change', function(){ changeHandler(this); } );
        }
    }
}

The this parameter that we're passing to the changeHandler function here refers to the checkbox object assigned to variable e as we encounter them while iterating over the form elements. 我们在此处传递给changeHandler函数的this参数是指在遍历表单元素时遇到的分配给变量e的复选框对象。

DECLARE THE EVENT HANDLER 声明事件处理程序

OUTSIDE our window.onload function, we need to declare our checkbox onChange event handler: window.onload函数之外,我们需要声明复选框onChange事件处理程序:

function changeHandler( elem )
{
    var usporedi = document.getElementById('usporedi');

    if( countChecked() == 2 )
    {
        disableCheckboxes();
        usporedi.disabled = false;
    }
    else
    {
        enableCheckboxes();
        usporedi.disabled = true;
    }
}

The handler in itself is very simple, but you'll notice that it makes use of 3 additional functions to get its job done. 处理程序本身非常简单,但是您会注意到它利用了3个附加功能来完成其工作。 countChecked() figures out how many checkboxes are currently checked: countChecked()计算出当前选中了多少个复选框:

function countChecked()
{
    var form = document.getElementById('myForm'), e, c = 0;

    for( i = 0, n = form.elements.length; i < n; i++ )
    {
        e = form.elements[i];

        if( e.type == 'checkbox' && e.id.indexOf('checkme') == 0 && e.checked == true )
        {
            c++;
        }
    }

    return c;
}

This function is executed each and every time a checkbox is changed. 每次更改复选框时,都会执行此功能。 If it returns a value of 2 back to changeHandler() , the next function is called to make sure that no more can be checked, and the disabled property is removed from the submit button. 如果它向changeHandler()返回2的值,则调用下一个函数以确保无法检查更多内容,并且已禁用属性从changeHandler()按钮中移除。

function disableCheckboxes()
{
    var form = document.getElementById('myForm'), e;

    for( i = 0, n = form.elements.length; i < n; i++ )
    {
        e = form.elements[i];
        if( e.type == 'checkbox' && e.id.indexOf('checkme') == 0 && e.checked == false )
        {
            e.disabled = true;
        }
    }
}

disableCheckboxes() iterates over every form element in the same way the other functions do (noticing a pattern here?) and disables any checkboxes which are not currently checked. disableCheckboxes()以与其他函数相同的方式遍历每个表单元素(是否注意到此处的模式?)并禁用当前未选中的所有复选框。 Each time a checkbox is changed and LESS THAN 2 in the form are checked, changeHandler() calls the next function to make sure that the checkboxes are fully functional, and disables the submit button so that the form cannot be submitted: 每次更改复选框并检查表单中的LESS THAN 2以下时, changeHandler()调用下一个函数以确保该复选框功能齐全,并禁用changeHandler()按钮,从而无法提交表单:

function enableCheckboxes()
{
    var form = document.getElementById('myForm'), e;

    for( i = 0, n = form.elements.length; i < n; i++ )
    {
        e = form.elements[i];
        if( e.type == 'checkbox' && e.id.indexOf('checkme') == 0 )
        {
            e.disabled = false;
        }
    }
}

Now this is quite a verbose way of tackling this problem - but I wanted you to be able to appreciate the steps required to produce the solution you were looking for. 现在,这是解决该问题的一种冗长方式-但我希望您能够体会到生成所需解决方案所需的步骤。 Enjoy :) 请享用 :)

PS: Here's the fiddle in case you wanted to see it in the wild: PS:这是小提琴,以防您想在野外看到它:

Checkbox Comparison Madness 复选框比较疯狂

You are using ID for checkbox which must be unique for every html input . 您正在使用ID作为复选框,它对于每个html input都必须是唯一的。 So rather using ID we are now using class.Class can be repeat.So according to that class we are now changing the button. 因此,我们现在使用class.Class可以重复使用ID,所以现在根据该类我们更改按钮。

Change code something like this 像这样更改代码

print
   "<div id='proizvod'></br><a style='text-decoration:none; color:black;' class='two' href='proizvod.php?id=$id' >" . $row["naziv"] . "" .
   "<p><img src=" . $row["slika"] . " width='200px' height='200px' style='border-radius: 15px;'></p>" .
   "<p> Cijena za gotovinu: " . $row["cijena"] . " KN </p>" .
   "<pre id='pre1'>" . $row["opis"] . "</pre>" .
   "</a>**<input type='checkbox' id='checkme' class='checkboxClass'/>**</div>

And JS function to 和JS功能

<script language='javascript'>
                        var checker = document.getElementsByClassName('checkboxClass');
                        var usporedi = document.getElementById('usporedi');
                        // when unchecked or checked, run the function
                        checker.onchange = function () {
                            if (this.checked) {
                                usporedi.disabled = false;
                            } else {
                                usporedi.disabled = true;
                            }

                        }
                    </script>

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

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