1

コンテナにいくつかの入力フィールドがあり、ユーザーはこれらのフィールドを乗算できます。コンテナに基づいて入力フィールドの名前を変更する必要があります。

<div class="stations">
 <input type="radio" name="pp[prc][0][station][0][id]">
 <input type="radio" name="pp[prc][0][station][1][id]">
</div>

これは私の HTML フォームです。

$(".stations").each(function(sIndex){
//Loop thru all .station
  $("input:radio", $(this)).each(function(rIndex){
    //Loop thru all radio buttons inside that container and change their names accordingly
   $("input:radio", $(this)).attr('name','pp[prc]['+sIndex+'][stations]['+rIndex+'][id]');
  });
});

これを行うと、何らかの理由でユーザーが複製され<div class="station">、その内容がラジオボタンの名前に応じて変更されません。どこで間違っていますか?

4

2 に答える 2

1

を使用して$(selector, context)おり、入力要素に子を含めることはできません。次のようにコーディングする必要があります。

$(this).attr('name', '...');
于 2013-04-15T00:39:08.937 に答える
0

セレクターに問題があるようです。次のようにしてみてください。

//Loop through each .station
$(".stations").each(function(sIndex){

    //Loop through each radio buttons inside the container
    $(this).find("input:radio").each(function(rIndex){

        // Change the radio name attribute
        $(this).attr('name','pp[prc]['+sIndex+'][stations]['+rIndex+'][id]');
    });
});

ワーキングjsFiddle

すばやく表示できるように、画面に名前が表示された jsFiddle を次に示します。

于 2013-04-15T00:48:59.020 に答える