2017-12-04 7 views
0

자동 완성이 첫 번째 양식의 값을 채울 때 동일한 페이지의 다른 양식에서 다른 입력을 채우는 방법을 알아야합니다.자동 완성이 다른 양식 입력을 채우는 방법

예와 같이 동일한 페이지에 세 개의 양식이 있고 다른 양식을 채우기 위해 첫 번째 양식의 자동 완성 값에있는 ID를 채워야하는 경우 어떻게해야합니까? 여기

예제 코드 : 사용자가 아약스와 함께 제출 버튼을 쳤을 때

//First form, in the license input the autocomplete fill the license, owner, brand and the id 
<form method="post" name="first" id="first" class="form-horizontal"> 
    <div class="row form-group"> 
     <label class="col-sm-3 text-right">Car license number:</label> 
     <div class="col-sm-9"> 
      <input type="text" class="form-control" name="license" id="license"> // autocomplete input 
     </div> 
    </div> 
    <div class="row form-group"> 
     <label class="col-sm-3 text-right">Owner:</label> 
     <div class="col-sm-9"> 
      <input type="text" class="form-control" name="owner" id="owner"> 
     </div> 
    </div> 
    <div class="row form-group"> 
     <label class="col-sm-3 text-right">Brand:</label> 
     <div class="col-sm-9"> 
      <input type="text" class="form-control" name="brand" id="brand"> 
     </div> 
    </div> 
    <input type="hidden" id="idCar" name="idCar" /> // hidden input needed in the other forms 
    <input type="submit" id="sCar" class="btn btn-info" value="Save Car" /> 
</form> 

//Second form 
<form method="post" name="second" id="second" class="form-horizontal"> 
    <div class="row form-group"> 
     <label class="col-sm-3 text-right">Requested work:</label> 
     <div class="col-sm-9"> 
      <input type="text" class="form-control" name="reWork" id="reWork"> 
     </div> 
    </div> 
    <div class="row form-group"> 
     <label class="col-sm-3 text-right">How many miles in this diagnosis have the car:</label> 
     <div class="col-sm-9"> 
      <input type="text" class="form-control" name="miles" id="miles"> 
     </div> 
    </div> 
    <div class="row form-group"> 
     <label class="col-sm-3 text-right">Type of oil used:</label> 
     <div class="col-sm-9"> 
      <input type="text" class="form-control" name="oil" id="oil"> 
     </div> 
    </div> 
    <input type="hidden" id="idCar" name="idCar" /> // Hidden input needed to fill previously in the first form 
    <input type="submit" id="sDiag" class="btn btn-info" value="Save diagnosis" /> 
</form> 

등등 ... 페이지 양식입니다 .. 각 차량의 진단 및 수정을위한 다섯 개 가지 형태가 제출. 내 문제는 autocomplete가 첫 번째 양식의 값을 채울 때 다른 양식의 동일한 ID를 추가하는 방법입니다.

+0

당신이 라이센스 에서 값이 다른 2 이름 idCar에 삽입됩니다 의미합니까? – synz

+0

@synz mmm 아니오, 다른 양식에 값 idCar 을 삽입해야합니다. 예를 들어, 사용자가 라이센스 번호를 입력하면 12345가 자동 완성으로이 라이센스의 소유자를 브랜드 및 ID로 채 웁니다. 라이센스 자동차, 그래서 다음 양식에서 idCar (숨겨져있다) 첫 번째 양식의 idCar가 필요 –

답변

1

이와 비슷한?

$("#first [name='license']").on("change keyup",function(){ 
      $("#first [name='idCar']").val("123").trigger("change"); // i just make this like some autocomplete example 
}); 

$("#first [name='idCar']").on("change",function(){ 
     var val = $(this).val(); 
      $("[name='idCar']").not(this).val(val); 
}); 

jsfiddle : https://jsfiddle.net/synz/cc958dp4/

+0

예,하지만 자동 완성 Json에서 idCar 값을 잡을 수 있습니까?. 나는'this.value'로 시도하지만 idCar가 아닌 license의 값을 보여줍니다. –

+0

idCar에 대한 자동 완성 기능은 다음과 같습니다 :'this.form.idCar.value = ui.item.idCar;'나는 이것도 시도했습니다. 하지만 성공하지 못했습니다. –

+1

자동 완료 후. $ ("# first [name = 'idCar']"). trigger ("change"); 따라서 idcar 입력의 값이 변경되었음을 감지 할 수 있습니다. – synz