2016-07-14 30 views
0

所以我有一個下拉每個表單更新值elemnt的Javascript的變化更新的輸入值中的foreach

這裏是我的組選擇框

   echo $this->Form->input('groups', ['options' => $groups, 'id' => 'groups', 'class' => 'selectpicker', 'label' => false, "onchange"=>"updateGroupId(this.value)"]); 

下面的代碼是代碼,我產生形成

<table class="table table-striped"> 
     <tbody> 
     <?php foreach($allProducts as $product): ?> 
     <tr> 
      <td> 
       <form class="form-inline" name="uploadform-<?= $product->id ?>" id="uploadform-<?= $product->id ?>" onsubmit="return checkInput<?= $product->id ?>()" enctype="multipart/form-data" action="/upload/<?= $product->id ?>" method="post"> 
        <span class="btn btn-primary btn-file"> 
        <span><i class="zmdi zmdi-upload zmdi-hc-fw"></i><?= __('Upload CSV') ?></span> 
        <input id="browsefiles-<?= $product->id ?>" name="uploadcsv" type="file" required="required" /> 
        <span id="errornotification-<?= $product->id ?>" class="hidden"><?= __('Select CSV File') ?></span> 
        </span> 
        <input type="text" name="group_id-<?= $product->id ?>" id="group_id-<?= $product->id ?>" value="<?= key($groups) ?>" /> 
       </form> 
      </td> 
     </tr> 
     <?php endforeach; ?> 
     </tbody> 
    </table> 

這裏是我的javascript代碼

<?php 
$i=0; 
foreach ($allProducts as $product) { 
?> 
    function updateGroupId(ish){ 
    document.forms["uploadform-<?= $product->id ?>"]["group_id-<?= $product->id ?>"].value = ish; 
    } } ?> 

使用此代碼,我可以僅更新上次輸入的更改值,但我需要所有輸入(groups_id)。

回答

0

我已經想出了一個答案,所以如果有人需要解決這裏它是!

所以,這是如何使用選擇框(on.change功能)

選擇框 選擇輸入需要相同改變所有的輸入值 - >

"onchange"=>"updateGroupId(this.value)" 

輸入變化

<input type="hidden" name="group_id" value="<?= key($groups) ?>" /> 

和JS代碼

function updateGroupId(ish){ 
     var group_ids = document.getElementsByName('group_id'); 
     for (var i=0;i<group_ids.length;i++) { 
     group_ids[i].value = ish; 
     } 
    }