[英]How to replace occurance of a string in a variable containing jquery captured html?
我在替換包含捕獲的html的變量中的某些值時遇到麻煩。
我將讓代碼解釋我要完成的工作。
免得說:
last_id = 1;
data.id = 2;
編碼:
/*prepare new edit,delete button*/
var controls = $('table#option_groups tr:last td:last').html();
輸出var控件將返回:
<a href="stuff/edit/1">Edit</a>
<a href="stuff/delete/1">Delete</a>
嘗試替換\\更新編輯按鈕:
//replacing edit button
controls.replace('/edit/'+last_id, '/edit/'+data.id);
替換后輸出控件:
<a href="stuff/edit/1">Edit</a>
<a href="stuff/delete/1">Delete</a>
所需的輸出:
<a href="stuff/edit/2">Edit</a>
<a href="stuff/delete/1">Delete</a>
因此,問題是:如何替換包含捕獲的html的變量中字符串的出現。
謝謝!
看來您沒有將替換后的字符串重新分配給controls
變量,請嘗試以下操作:
controls = controls.replace('/edit/'+last_id, '/edit/'+data.id);
您可以在一行代碼中完成此操作:
var controls = $('table#option_groups tr:last td:last').html().replace("/edit/"+last_id, "/edit/"+data.id);
輸出:
<a href="stuff/edit/2">Edit</a>
<a href="stuff/delete/1">Delete</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.