2012-03-20 11 views
1

の一部を置き換えます。jQueryのは、私は次のコードを持っている文字列

​<div id="container"> 
<input type="text" name="a1" id="a1"> 
<input type="text" name="a2" id="a2"> 
​</div>​​​​​​​​​​​​​​​​ 

をそして私は、テキスト内のすべての要素のidとnameプロパティに「」を「B」のすべてのインスタンスを置き換えたいですDIV ID =「コンテナ」

ので、新しいコードは次のようにする必要があります:

​<div id="container"> 
<input type="text" name="b1" id="b1"> 
<input type="text" name="b2" id="b2"> 
​</div>​​​​​​​​​​​​​​​​ 

私はちょうどそれが(置き換えるJavaScriptを使用して動作させることができるように見えることはできません)。

+4

私たちにあなたが既に試したコードを表示します。また、 'のようなマークアップで終わるので、_all_' a'を 'b'に置き換えたくないということは明らかです。実際に何をしようとしていますか? –

答えて

6
$('#container input').each(function(){ // Loop through all inputs 
    this.name = this.name.replace('a', 'b'); // Replace name 
    this.id = this.id.replace('a', 'b'); // Replace ID 
}); 

DEMO:http://jsfiddle.net/G3vCf/

1
$('#a1').attr('name', 'b1').attr('id', 'b1'); 
$('#a2').attr('name', 'b2').attr('id', 'b2'); 
0
$("#container").children("input").each(function() { 
    var name = $(this).attr("name"); 
    var id = $(this).attr("id"); 

    name = name.replace(/a/g, "b"); 
    id = id.replace(/a/g, "b"); 

    $(this).attr("name", name); 
    $(this).attr("id", id); 
}); 
0
ここ

$(function(){ 
    var html =$('#container').html().replace(/a/g,'b'); 
    $('#container').html(html);​ 
}); 

Example

関連する問題