简体   繁体   中英

How to replace occurance of a string in a variable containing jquery captured html?

I have trouble with replacing certain values in a variable that contains captured html.

I'll let the code explain what I want to accomplish.

lest say:

last_id = 1;
data.id = 2;

the code:

/*prepare new edit,delete button*/
var controls = $('table#option_groups tr:last td:last').html();

outputting var controls will return:

<a href="stuff/edit/1">Edit</a>
<a href="stuff/delete/1">Delete</a>

trying to replace\\update edit button:

//replacing edit button
controls.replace('/edit/'+last_id, '/edit/'+data.id);

outputting controls after replace:

<a href="stuff/edit/1">Edit</a>
<a href="stuff/delete/1">Delete</a>

desired output:

<a href="stuff/edit/2">Edit</a>
<a href="stuff/delete/1">Delete</a>

So the question is: How do you replace occurance of string in a varible that contains captured html.

Thank you!

看来您没有将替换后的字符串重新分配给controls变量,请尝试以下操作:

controls = controls.replace('/edit/'+last_id, '/edit/'+data.id);

You can do this in one line of code:

var controls = $('table#option_groups tr:last td:last').html().replace("/edit/"+last_id, "/edit/"+data.id);

Output:

<a href="stuff/edit/2">Edit</a>
<a href="stuff/delete/1">Delete</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM