我在输入文件中有这个字符串。
<input type="file" data-url="/modules/wizard/upload.php?eid=18000115&type=ico&case=protagonist&id=121001118" value="" class="wizard_image" name="files">
data-url="/modules/wizard/upload.php?eid=18000115&type=ico&case=protagonist&id=121001118"
现在,我只想用不同的内容更改该字符串的最后一个参数:id=121001118
,而不是 data-url
属性的整个值。
我该怎么做?下面的字符串将更改整个字符串,这不是我想要的。
newBox.find('input.wizard_image').attr('data-url', 'somethingElse');
感谢您的帮助
最佳答案
您可以使用 regular expression :
newBox.find('input.wizard_image').attr('data-url', function(i, val) {
return val.replace(/id=\d+$/, 'id=somethingElse');
});
Passing a function to .attr
可以轻松修改现有值。
表达式解释:
id= // literally matches "id="
\d+ // matches one or more digits
$ // matches the end of the line/string
关于javascript - jQuery 仅更改字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17405602/