2015-03-13 63 views
1

我正在使用ajax請求進行自動填充,對靜態字段工作正常。 例如:在動態字段上自動填充ajax響應

$this->registerJs("$(document).delegate('.form-control','change',function(event){ 

    $.ajax({ 
     url: '".yii\helpers\Url::toRoute("ot-note/instrument1")."', 
     dataType: 'json', 
     method: 'GET', 
     data: {id: $(this).val(), 
     }, 
     success: function (data, textStatus, jqXHR) { 
      $('#otinstrumententry-0-instrument_code').val(data.instrument_code); 

      }, 

     beforeSend: function (xhr) { 
      alert('loading!'); 
     }, 
     error: function (jqXHR, textStatus, errorThrown) { 
      console.log('An error occured!'); 
      alert('Error in ajax request'); 
     } 
    }); 
});"); 

我的農田動態添加,它是不工作的動態添加的領域,代碼是這樣的: 領域的id變得像 #field-0-instrument_code #field-1-instrument_cdoe

除了第一個字段,連續的字段不會被Jquery識別,而是被動態添加的。

我該如何解決這個問題。 謝謝。

此外動態領域

<div id="instrument_entry"> 
    <h3>Instruments Used</h3> 
    <?php $id = 0; ?> 

    <?php foreach ($otinstrumentModels as $otinstrument) { ?>  

     <div id="language" class="work-data-pad brdr-work marbtm10 row"> 
      <div class="col-md-4">  
      <?= $form->field($otinstrument, '[' . $id . ']' . 'instrument_name')->DropDownList(ArrayHelper::map(\app\models\Instrument::find()->all(), 'id', 'instrument_name'), 
[ 'prompt' => 'Please Select' ])?>  
      </div> 
      <div class="col-md-2">  
      <?= $form->field($otinstrument, '[' . $id . ']' . 'instrument_code')->textInput(['maxlength' => 255,'class'=>'form-control']) ?>  
      </div> 
      <div class="col-md-1">  
      <?= $form->field($otinstrument, '[' . $id . ']' . 'hrs_time')->textInput(['maxlength' => 255])->label('Hrs-Time') ?>  
      </div> 
      <div class="col-md-2">  
      <?= $form->field($otinstrument, '[' . $id . ']' . 'total_charges')->textInput(['maxlength' => 255]) ?>  
      </div> 
      <?php ?> 
    <div style="margin-top: 30px;" class="col-md-3 <?php echo ($id < 1) ? 'dnone' : 'dblock'; ?>" id="divDelete" class="row-fluid">   
    <a class="ft11 btn-remove" onclick="deleteSection(this, 'instrument_entry');"><span class="marleft18">Remove</span></a>    
    </div> 
</div> 
    <?php $id++; ?> 
    <?php } ?> 
</div> 

生成的HTML

<div id="instrument_entry"> 
    <h3>Instruments Used</h3> 



     <div id="language" class="work-data-pad brdr-work marbtm10 row"> 
      <div class="col-md-4">  
      <div class="form-group field-otinstrumententry-0-instrument_name"> 
<label class="control-label" for="otinstrumententry-0-instrument_name">Instrument Name</label> 
<select id="otinstrumententry-0-instrument_name" class="form-control" name="OtInstrumentEntry[0][instrument_name]"> 
<option value="">Please Select</option> 
<option value="1">IMPLANTS(ORTHOPEDIC)</option> 
<option value="2">O2 CHARGES PER HOUR</option> 
</select> 

<div class="help-block"></div> 
</div>  
      </div> 
      <div class="col-md-2">  
      <div class="form-group field-otinstrumententry-0-instrument_code"> 
<label class="control-label" for="otinstrumententry-0-instrument_code">Instrument Code</label> 
<input type="text" id="otinstrumententry-0-instrument_code" class="form-control" name="OtInstrumentEntry[0][instrument_code]" maxlength="255"> 

<div class="help-block"></div> 
</div>  
      </div> 
      <div class="col-md-1">  
      <div class="form-group field-otinstrumententry-0-hrs_time"> 
<label class="control-label" for="otinstrumententry-0-hrs_time">Hrs-Time</label> 
<input type="text" id="otinstrumententry-0-hrs_time" class="form-control" name="OtInstrumentEntry[0][hrs_time]" maxlength="255"> 

<div class="help-block"></div> 
</div>  
      </div> 
      <div class="col-md-2">  
      <div class="form-group field-otinstrumententry-0-total_charges"> 
<label class="control-label" for="otinstrumententry-0-total_charges">Total Charges</label> 
<input type="text" id="otinstrumententry-0-total_charges" class="form-control" name="OtInstrumentEntry[0][total_charges]" maxlength="255"> 

<div class="help-block"></div> 
</div>  
      </div> 
       <div style="margin-top: 30px;" class="col-md-3 dnone" id="divDelete" class="row-fluid">   
    <a class="ft11 btn-remove" onclick="deleteSection(this, 'instrument_entry');"><span class="marleft18">Remove</span></a>    
    </div> 
</div> 

    </div> 

    <div class="row"> 
     <div class="col-md-12"> 
      <button type="button" class="btn btn-primary sec-btn marbtm10" onclick="addNewSection('instrument_entry', 'OtInstrumentEntry')">+ Add Instrument</button> 
     </div> 
    </div> 

    <div class="row" style="margin-top: 20px;"> 
     <div class="col-md-12"> 
      <button type="submit" class="btn btn-success">Create</button>  </div> 
    </div> 

    </form> 
+0

我認爲,您應該爲您的問題添加一個PHP標記。 – Pankaj 2015-03-13 12:18:51

回答

0

第一個代碼,你可以試試這個:

$this->registerJs("$(document).delegate('.form-control','change',function(event){ 
    sendAjax(this); 
});"); 

function sendAjax(element) 
{ 
    $.ajax({ 
     url: '".yii\helpers\Url::toRoute("ot-note/instrument1")."', 
     dataType: 'json', 
     method: 'GET', 
     data: {id: $(element).val()}, 
     success: function (data, textStatus, jqXHR) { 
      $('#otinstrumententry-0-instrument_code').val(data.instrument_code); 

      }, 

     beforeSend: function (xhr) { 
      alert('loading!'); 
     }, 
     error: function (jqXHR, textStatus, errorThrown) { 
      console.log('An error occured!'); 
      alert('Error in ajax request'); 
     } 
    }); 
} 

現在你可以添加onchange="sendAjax(this)"新添元素。我認爲這個解決方案可以解決你的問題。

0

代碼中只有一個硬編碼值(在success處理程序中),所以我會假設這是問題來自的地方。你可以使用.attr("id")來獲取當前域ID,使用正則表達式來獲取數並用它來生成選擇:

var currentId = $(this).attr("id"); 
    var generatedId = ""; 
    if (currentId.match(/otinstrumententry-\d+-/)) { 
     generatedId = "#" + currentId.match(/otinstrumententry-\d+-/)[0] 
          + "instrument_code"; 
    } 
    $.ajax({ 
     // ... 
     success: function (data, textStatus, jqXHR) { 
      if (generatedId !== "") { 
       $(generatedId).val(data.instrument_code); 
      } 
     } 
    }); 

此外,值得注意的正是你想在調用這個左撇子哪些元素。從它的外觀,它看起來像它只是select S,但我不能肯定全部不知道的商業邏輯:

$(document).delegate('select.form-control','change',function(event){ 
}); 

在一個側面說明,如果你使用jQuery 1.7或更新版本,您應該用.on代替.delegate。如果您使用的是舊版本,則應考慮更新它

+0

嗨bigt - 我試過你的建議,ajax調用正確觸發,但不是填充'instrument_code'字段中的數據,'下拉式選擇字段變白。 – Joshi 2015-03-13 10:56:16

+0

嗨Bigt - 我已經更新了這個問題。 – Joshi 2015-03-13 11:03:37

+0

好的,我進一步更新了這個問題,我認爲這是你需要的。 – Joshi 2015-03-13 11:11:53