2014-10-06 85 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的东西很新,但至少我能找到的一个解决办法验证该行提供。