2010-12-30 10 views
2

jQueryを使用してCakePHPでフォームを作成しています。私の問題は、フォームのいくつかのフィールドをjQueryでクローンする必要があることです。フォームは予約であり、1つ以上の顧客(グループの場合)を追加するには、顧客の詳細を複製する必要があります。フォーム内のjQueryクローン()問題

var idx = 0; 
     $(document).ready(function() { 
      $("#addCustomerFields").click(function() { 
       idx = idx + 1; 
       var x = $("#Cliente").clone(); 
       $(x) 
        .attr("id", "Client." + idx) 
        .find("label").each(function() { 
         $(this).attr("for", $(this).attr("for").replace(".0", "." + idx)); 
        }) 
        .end() 
        .find("input").each(function() { 
         $(this) 
          .attr("id", $(this).attr("id").replace(".0", "." + idx)) 
          .attr("name", $(this).attr("name").replace("[0]", "[" + idx + "]")); 

         //this.value = ''; 
        }) 

       $("#CustomerFields").append(x); 
      }); 
     }); 

それはOKだけのテキスト入力フィールドで動作します:

私はこのコードを使用しています。ドロップダウン(選択)は機能しません。 select(idとname)の属性はスクリプトによって変更されていないようです。私が2人の顧客を追加すると、最初に男性1人、データベースに2人目女性1人が女性として出現し、2人目はセックスなしです。各フィールドには名前とIDがあります:クライアント[0] [ファーストネーム]、クライアント[0] [性別]などです。クローンすると、

図である。

<fieldset id="Cliente"> 
     <legend class="legend"><?php __('Client Info'); ?></legend> 
    <?php 

     //this is select 
     echo $this->Form->input('Client.0.sex_id', array(
      'label'=>'Gender', 
      'div'=>'float IconSex', 
      ) 
     ); 
     echo $this->Form->input('Client.0.firstname', array(
      'label'=>'First name', 
      'div'=>'float IconUser' 
      ) 
     ); 
     echo $this->Form->input('Client.0.lastname', array(
      'label'=>'Last name', 
      'div'=>'float IconUser' 
      ) 
     ); 

     //another select 
     echo $this->Form->input('Client.0.country_id', array(
      'label'=>'Country', 
      'div'=>'clear IconCountry', 
      'default'=>'121' 
      ) 
     ); 

    ?> 

    </fieldset><div id="CustomerFields"></div> 

    <?php 
    echo $this->Html->div('IconAdd', 'Add customer.', array(
      'id'=>'addCustomerFields', 
      ) 
     ); 
    ?> 

任意のアイデア?

あなただけ .find("input").each(function(...

.find(":input")matchへのすべての入力(選択、入力、テキストエリアなど)

またはちょうどに

$('select, input')

を使用してみてください、あなたの現在のコードで入力要素を選択している

答えて

1

入力と選択を一致させる

+0

ありがとう!それは.find( ":input")で動作します:) – Michal

関連する問題