Jump to content
php.lv forumi
  • 0

replace all ids, names etc.


renathy

Question

Ir sekojošs kods:


$(cloned).find("*").each(function(index, element) {
  if(element.id)
  { 
   //replace id
   var id = element.id;
   var newId = nextItemId;                
id = id.replace("Def_[0-9]+_", "Def_"+nextItemId+"_");
element.id = id; 

//replace name
   var name = element.name;      
name = name.replace("Def[[0-9]][", "Def["+nextItemId+"][");
element.name = name;
  }
});

 

Idejiski viam vajadzētu iziet cauri visiem elementa "cloned" (kas iepriekš korekti tiek noteikts) elementiem un visur aizvietot "id", "name" un ja tas ir label, tad arī "for" ar jaunām vērtībām.

 

Piemēram, ja nexItemId = 5:

 

old id = "Def_1_name", tad new_id = "Def_5_name"

old name = "Def[1][name], tad new name = "Def[5][name]"

old for tage = "Def[1][name], tad new for tage = "Def[5][name]"

 

Tikai diemžēl augstākminētajā kodā ir kaut kur kļūda un "replace" nestrādā + kā labāk nonākt pie "for" taga?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Palasam par replace(), pievēršot uzmanību regexp daļai.

 

 

$(":input", cloned).map(function(){
 $(this).attr({
   id: $(this).attr("id").replace(/_\d+_/, "_"+nextItemId+"_"),
   name: $(this).attr("name").replace(/\[\d+\]/, "["+nextItemId+"]")
 });
});
$("label", cloned).map(function(){
 $(this).attr("for", $(this).attr("for").replace(/_\d+_/, "_"+nextItemId+"_"));
});

Edited by marcis
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...