我在输入文件中有这个字符串。
<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');
感谢您的帮助
答案 0 :(得分:4)
您可以使用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
答案 1 :(得分:0)
我会使用字符串函数:substring
,replace
。
var str = 'data-url="/modules/wizard/upload.php?eid=18000115&type=ico&case=protagonist&id=121001118"';
var id = str.substring(str.indexOf(";id=") + 4);
str = str.replace(id, "something...");
但是,更好的解决方案是使用正则表达式。
答案 2 :(得分:0)
var newID = 123435465; // the new Id you'd like to put into the URL
var $el = newBox.find('input.wizard_image'); // take the reference of the element
var oldURL = $el.data('url'); // get the data-url
var newURL = oldURL.replace(/id=[0-9]+/, newID);// replace the id=number pattern by newID
$el.data('url', newURL); // set it to a new one by replacing
答案 3 :(得分:0)
使用正则表达式的最简单方法
newBox.find('input.wizard_image').attr('data-url',
newBox.find('input.wizard_image').replace(/id\=[0-9]{0,}/gi, "something-else")
);