JQuery在提交表单时触发

时间:2014-10-06 09:36:42

标签: php jquery html

我目前正在创建一个页面,您可以在其中填写有关损坏的汽车/货物的损坏索赔。您有3个选项(driver / subcontractor / personel),JQuery用于根据所选选项显示/隐藏不同的文本框。

这里变得棘手。一切都很好,直到我提交表格。当我这样做时,再次触发changeVehicle JQuery函数并再次在licensePlate中重新加载原始值,即使我在表单中更改了它。结果是我得到了我的牌照的旧变量。

有没有人知道为什么会这样?

我的代码:

<?php

...

<form id="form" class="form-horizontal" method="post" action="<?php echo $url?>&action=edit<?php echo $edit?>" enctype="multipart/form-data">
    <div class="control-group">
        <label class="control-label" for="filenr"><?php echo $lng->show("claim_file_nr")?></label>
        <div class="controls">
            <input type="text" id="filenr" name="filenr" value="<?php echo $file_nr?>" disabled>
            <input type="hidden" id="file_nr" name="file_nr" value="<?php echo $file_nr?>" />
        </div>
    </div>

    <div class="control-group">
        <label class="control-label" for="damage_type"><?php echo $lng->show("claim_damage_type")?>*</label>
        <div class="controls">
            <select id="damage_type" name="damage_type" <?php if ($par["id"] != 0) { echo "disabled"; } ?>>
                <option value="0" <?php if ($damage_type == 0) echo "selected" ?>><?php echo $lng->show("claim_type_vehicle") ?></option>"
                <option value="1" <?php if ($damage_type == 1) echo "selected" ?>><?php echo $lng->show("claim_type_goods") ?></option>"
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="date_damage"><?php echo $lng->show("claim_date_damage"); ?>*</label>
        <div class="controls">
            <div class="input-append">
                <input class="input-small" type="text" id="date_damage" name="date_damage" data-date-format="dd-mm-yyyy" placeholder="dd-mm-yyyy" 
                        value="<?php echo $cfunc->convertDateFromDB(substr($date_damage, 0, 10)) ?>" required>
                <span class="add-on"><i class="icon-calendar"></i></span>
            </div>
        </div>
    </div>  
    <div class="control-group">
        <label class="control-label" for="description"><?php echo $lng->show("claim_description")?>*</label>
        <div class="controls">
            <textarea class="input-xxlarge" rows="4" id="description" name="description" required><?php echo $description;?></textarea>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="choice"><?php echo $lng->show("claim_choice")?>*</label>
        <div class="controls controls-row">
            <select id="choice" name="choice" onchange="changeChoice();">
                <option value="1" <?php if (isset($driver)&& $driver != 0) echo "selected" ?>><?php echo $lng->show("claim_driver") ?></option>"
                <option value="2" <?php if (isset($subcontractor)&& $subcontractor != 0) echo "selected" ?>><?php echo $lng->show("claim_subcontractor") ?></option>"
                <option value="3" <?php if ($personel != "") echo "selected" ?>><?php echo $lng->show("claim_personel") ?></option>"
            </select>
        </div>
    </div>

    <div id="driver-container" class="control-group">
        <label class="control-label" for="driver"><?php echo $lng->show("claim_driver")?>*</label>
        <div class="controls controls-row">
            <select id="driver" name="driver">
                <?php 
                    $result = $db->q("SELECT * FROM ERP_drivers");
                    foreach ($result as $a) {
                        if ($driver == $a["driver_id"]) {
                            echo "<option value=".$a["driver_id"]." selected>".$a["driver_name"]."</option>";
                        }else{
                            echo "<option value=".$a["driver_id"].">".$a["driver_name"]."</option>";
                        }
                    }
                ?>
            </select>
        </div>
    </div>
    <div id="subcontractor-container" class="control-group">
        <label class="control-label" for="subcontractor"><?php echo $lng->show("claim_subcontractor")?>*</label>
        <div class="controls controls-row">
            <select id="subcontractor" name="subcontractor">
            <?php
                $subcontractors = $db->q("SELECT r.relatie_id, r.relatie_naam 
                                    FROM relaties AS r INNER JOIN relatie_lijsten AS l
                                    ON r.relatie_id = l.relatie_lijst_relatie_id
                                    WHERE l.relatie_lijst_relatieslijst_id = 23");
                foreach($subcontractors as $s){
                    if ($subcontractor == $s["relatie_id"]) {
                        echo "<option value=\"".$s["relatie_id"]."\" selected>".$s["relatie_naam"]."</option>";
                    }else{
                        echo "<option value=\"".$s["relatie_id"]."\">".$s["relatie_naam"]."</option>";
                    }
                }
            ?>
            </select>   
        </div>
    </div>
    <div id="personel-container" class="control-group">
        <label class="control-label" for="personel"><?php echo $lng->show("claim_personel")?>*</label>
        <div class="controls controls-row">
            <select id="personel" name="personel">
            <?php
                $personelList = $db->q("SELECT * FROM `erp_gebruiker` WHERE `actief` =1");
                foreach($personelList as $p){
                    if ($personel == $p["gebruiker_id"]) {
                        echo "<option value=\"".$p["gebruiker_id"]."\" selected>".$p["naam"]."</option>";
                    }else{
                        echo "<option value=\"".$p["gebruiker_id"]."\">".$p["naam"]."</option>";
                    }
                }
            ?>
            </select>   
        </div>
    </div>

    <div id="vehicle-container" class="control-group">
        <label class="control-label" for="vehicle"><?php echo $lng->show("claim_choice_vehicle")?>*</label>
        <div class="controls controls-row">
            <select id="vehicle" name="vehicle" onchange="changeVehicle();">
                <?php 
                    $result = $db->q("SELECT * FROM ERP_vehicles");
                    foreach ($result as $v) {
                        if ($vehicle == $v["vehicle_id"]) {
                            echo "<option value=".$v["vehicle_id"]." selected>".$v["vehicle_name"]."</option>";
                        }else{
                            echo "<option value=".$v["vehicle_id"].">".$v["vehicle_name"]."</option>";
                        }
                    }
                ?>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="license_plate"><?php echo $lng->show("claim_license_plate")?></label>
        <div class="controls">
            <input type="text" id="license_plate" name="license_plate" value="<?php echo $license_plate; ?>">
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="faulty"><?php echo $lng->show("claim_faulty")?></label>
        <div class="controls controls-row">
            <select id="faulty" name="faulty">
                <option value="0" <?php if ($faulty == 0) echo "selected"; ?>><?php echo $clng->show("no")?></option>";
                <option value="1" <?php if ($faulty == 1) echo "selected"; ?>><?php echo $clng->show("yes")?></option>";
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="file_insurance"><?php echo $lng->show("claim_insurance_nr")?></label>
        <div class="controls">
            <input type="text" id="file_insurance" name="file_insurance" value="<?php echo $file_insurance?>">
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="damage_amount"><?php echo $lng->show("claim_damage_amount")?></label>
        <div class="controls">
            <input type="number" step="0.01" id="damage_amount" name="damage_amount" value="<?php echo $damage_amount?>">
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="exemption"><?php echo $lng->show("claim_exemption")?></label>
        <div class="controls">
            <input type="number" step="0.01" id="exemption" name="exemption" value="<?php echo $exemption?>">
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="amount_payed"><?php echo $lng->show("claim_amount_payed")?></label>
        <div class="controls">
            <input type="number" step="0.01" id="amount_payed" name="amount_payed" value="<?php echo $amount_payed?>">
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="status"><?php echo $lng->show("claim_status")?></label>
        <div id="choice" class="controls">
            <select id="status" name="status">
                <option value="1" <?php if ($status == 1) echo "selected"; ?>><?php echo $lng->show("claim_status_handling")?></option>";
                <option value="2" <?php if ($status == 2) echo "selected"; ?>><?php echo $lng->show("claim_status_handled")?></option>";
            </select>
        </div>
    </div>

    <div class="form-actions">
        <button type="submit" class="btn btn-primary"><?php echo $knop?></button>
        <button type="button" class="btn" onclick="javascript:location.href='<?php echo $url?>'"><?php echo $clng->show("cancel")?></button>
    </div>
</form>

<script>
    $(function () { 
        $("input,select,textarea").not("[type=submit]").jqBootstrapValidation();
        changeChoice();
//      alert("blablabla");

    });

    $("#date_damage").datepicker({format:'dd-mm-yyyy'});

    function changeChoice() {
        var c = $("#choice :selected").val();

        if (c == "1") {
            $("#driver-container").show();
            $("#subcontractor-container").hide();
            $("#personel-container").hide();
        } else if (c == "2") {
            $("#driver-container").hide();
            $("#subcontractor-container").show();
            $("#personel-container").hide();
        } else {
            $("#driver-container").hide();
            $("#subcontractor-container").hide();
            $("#personel-container").show();
        }


        changeVehicle()
    }

    function changeVehicle() {
        var choice = $("#choice :selected").val();
        var vehiclePlate = $("#vehicle :selected").val();
        var licensePlate = '<?php if ($par["id"] == 0 ) { echo ""; } else { echo $license_plate; }?>'

        if (choice == "1" || choice == "3") {
            $("#vehicle-container").show();
            $("#license_plate").prop('disabled', true);
            showLicense(vehiclePlate);
            $("#license_plate").val(data[0]);
        } else {
            $("#vehicle-container").hide();
            $("#license_plate").prop('disabled', false);
            //$("#license_plate").val(licensePlate);
        }
    }

    function showLicense(plate) {
        $.ajax({
            type: 'POST',
            url: 'index.php?page=claims&ajax=getplate',
            dataType: 'json',
            data: { plate: plate },
            success: function(data) {
                $("#license_plate").val(data[0]);
            }
        });
    }
</script>

1 个答案:

答案 0 :(得分:0)

找到它。似乎有些验证码是坏人。当我把它放在评论中时,一切正常:

<script>
    $(function () { 
        //$("input,select,textarea").not("[type=submit]").jqBootstrapValidation();
        changeChoice();
    });

...

</script>

我真的不知道为什么,我仍然对这个JavaScript / JQuery的东西很新,但至少我可以找到一个解决方法来提供该行的验证。