2014-10-06 83 views
0

我目前正在創建一個頁面,您可以在其中填寫損壞的汽車/貨物的損壞聲明。您有3個選項(驅動程序/分包商/ personel),JQuery用於根據選定的選項顯示/隱藏不同的文本框。JQuery在提交表單時觸發

這裏是棘手的地方。一切工作都很好,直到我提交表格。當我這樣做時,再次觸發changeVehicle JQuery函數,並重新載入licensePlate中的原始值,即使在我修改了它的形式後。結果是我得到了我的車牌的舊變量。

有沒有人有一個想法,爲什麼它refires?

我的代碼:

<?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> 
+0

您所遇到的可能是典型的「自動完成」行爲。嘗試向您的'select'元素添加一個HTML屬性'autocomplete =「off」'。 – Mysteryos 2014-10-06 10:03:05

+0

感謝您的反饋,但它不起作用。我認爲解決方法在於防止在提交時觸發該特定功能,因爲當我在該功能中添加警報時,它還會在提交時顯示警報。但我似乎無法弄清楚在哪裏和/或爲什麼這樣做。 – Daemun 2014-10-06 10:48:45

回答

0

發現了它。似乎有些驗證代碼是壞人。一切工作正常,當我把這個評論:

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

... 

</script> 

我真的沒有一個想法,爲什麼,我還是有這個JavaScript/JQuery的東西很新,但至少我能找到的一個解決辦法驗證該行提供。