第二次单击时触发jquery事件

时间:2016-02-12 15:45:34

标签: javascript jquery onchange

我遇到的jQ事件只会在第二次clic上被触发

(jQuery(' .CHARGE输入')。添加(' .DISPO')。更改(功能majForm(e)

我搜索谷歌和搜索引擎优化,但我找不到任何工作。 这是代码:

$(document).ready(function ()
{
//This allows to get the right color, as well as totalCharge value on page load
    jQuery('.SOLDE').each(function()
    {
        if (jQuery(this).val() < 0)
        {
            jQuery(this).css('background', 'red');
        }
    })

        var total=0;

    jQuery('.CHARGE input').each(function()
    {
        val = jQuery(this).val();
        if (val != "" ) 
        {
            total += parseFloat(val);
        };
    })

    jQuery('.CHARGE input').add('.DISPO').change(function majForm(e)
    {
        /*form sent as soon as one of the values is modified*/
        e.preventDefault();
        $.ajax
        ({
            type: 'post',
            url: 'envoi_dispo_semaine.php',
            data: $('form').serialize(),
            success: function () 
            {
                // alert('Le planning a été mis à jour.');
            }
        });

        var maxCharge = function () 
        {
            $.ajax
            ({
                type: 'post',
                url: 'check_charge.php',
                data: $('form').serialize(),
                dataType: 'json',
                success: function (data) 
                {
                    tmp = data;
                }
            });
            return tmp;
        }();

        var total=0;

        jQuery('.CHARGE input').each(function()
        {
            val = jQuery(this).val();
            if (val != "" ) 
            {
                total += parseFloat(val);
            };
        })

        if (total > maxCharge)
        {
            alert('Vous avez planifié plus que nécessaire. La charge de la tâche est de ' + maxCharge)
        };

        //I'm getting the values of all the fields. It's not a regular input based table so this is the only way I've found to do it.
        var jDispo  = {};
        var jCharge = {};
        var jSolde  = {};
        var d = 0;
        var c = 0;
        var s = 0;

        jQuery('.DISPO').each(function()
        {
            d = jQuery('.DISPO').index(this);
            jDispo[d] = jQuery(this).val();
            // alert(d);
        });

        jQuery('.SOLDE').each(function()
        {
            var val = jQuery(this).val();
            if (val != "" ) 
            {
                s = jQuery('.SOLDE').index(this);
                jSolde[s] = val;
                // alert(s);
            };
        });

        jQuery('.CHARGE input').each(function()
        {
            var val = jQuery(this).val();
            if (val != "" ) 
            {
                // alert(val);
                c = jQuery('.CHARGE input').index(this);
                jCharge[c] = val;
                // alert(c);
                if (jCharge[c] < 0) 
                {
                    jQuery(this).css('background', 'red');
                } else {
                    jQuery(this).css('background', '');
                };
            };
        });
        /*Append the correct solde values to the correct inputs*/
        for (var i = 0; i < c+1; i++) 
        {
            if (jCharge[i] != "") 
            {
                jSolde[i] = jDispo[i] - jCharge[i];
            };
        };

        jQuery('.SOLDE').each(function()
        {
            var k = jQuery('.SOLDE').index(this);
            jQuery(this).val(jSolde[k]);

            if (jSolde[k] < 0) 
            {
                jQuery(this).css('background', 'red');
            } else {
                jQuery(this).css('background', '#e8edff');
            };

            if (isNaN(jQuery(this).val())) 
            {
                jQuery(this).val('');
            };
        });
    });
});

这是html代码:

<thead>
            <?php
                echo "  <td colspan=2>Semaine n°</td>
                        <td>Retard</td>";
                for ($i=$numerosemaine; $i <= $numerosemaine + $longueurAff; $i++) 
                {
                    echo "<form action=\"envoi_dispo_semaine.php\" method=\"post\">
                            <td>
                                <input type=\"hidden\" name=\"semaine_id\" value=\"".$i."\" />".$i."</td>";
                }

            ?>
            </thead>
            <tbody>
            <?php 
                foreach($users as &$myUser)
                {
                        echo "  <tr>
                                    <td width=66% rowspan=3><input type=\"hidden\" name=\"login\" value=\"".$myUser."\" onblur=\"updateCharge\"/>".$myUser."</td>
                                    <td width=34%>Disponibilité</td>
                                    <td rowspan=3></td>
                            ";
                                    for ($i=$numerosemaine; $i <= $numerosemaine + $longueurAff; $i++) 
                                    {
                                        $req = "
                                                SELECT Nb_max_jours FROM Dispo_par_semaine WHERE login = '".$myUser."' AND semaine_id = ".$i;
                                        $query = requete_is_plancharges($req);
                                        $row = mysql_fetch_row($query);
                                        $affichageDispo = $row[0];
                                        echo "  
                                                <td>
                                                    <input id=\"dispo\" type=\"number\" name=\"disponibilite[]\" value=".$affichageDispo." min=\"0\" max=\"5\" step=\"0.5\" class=\"input\"/>
                                                </td>
                                            ";
                                    }
                                    echo"
                                </tr>
                                <tr>
                                    <td width=34%>Charge</td>";
                                    for ($i=$numerosemaine; $i <= $numerosemaine + $longueurAff; $i++) 
                                    { 
                                        $req = "
                                            SELECT charge FROM Charge_par_tache WHERE login = '".$myUser."' AND semaine_id = ".$i;
                                        $query = requete_is_plancharges($req);
                                        $row = mysql_fetch_row($query);
                                        $affichageCharge = $row[0];
                                        echo $affichageCharge."

                                    <td>
                                        <input id=\"CHARGE\" type=\"readonly\" name=\"charge[]\" onfocus=\"this.blur()\" style=\"background:#e8edff\" value=\"$affichageCharge\" min=\"0\" max=\"\" step=\"0.5\" class=\"input\"/>
                                    </td>";
                                    }
                                    echo"
                                </tr>
                                <tr>
                                    <td width=34%>Solde</td>";
                                    for ($i=$numerosemaine; $i <= $numerosemaine + $longueurAff; $i++) 
                                    {
                                        $req1 = "
                                                SELECT charge FROM Charge_par_tache WHERE login = '".$myUser."' AND semaine_id = ".$i;
                                        $req2 = "
                                                SELECT Nb_max_jours FROM Dispo_par_semaine WHERE login = '".$myUser."' AND semaine_id = ".$i;
                                        $query1 = requete_is_plancharges($req1);
                                        $row1 = mysql_fetch_row($query1);
                                        $query2 = requete_is_plancharges($req2);
                                        $row2 = mysql_fetch_row($query2);
                                        $solde=$row2[0]-$row1[0];
                                        echo "<td><input type=\"readonly\" class=\"SOLDE\" onfocus=\"this.blur()\" value=\"$solde\" style=\"background:#e8edff\"/></td>";
                                    }
                                        ?>
                                </tr>
                                        <?php
                }
            ?>

            </tbody>
        </table>
        </form> 

我想要做的是当用户修改CHARGE或DISPO值时,SOLDE val会相应更新(SOLDE = DISPO - CHARGE)。所有这些值都附加到输入(数字或只读)。

0 个答案:

没有答案