<script type="text/javascript">
$(this).ready( function() {
$("#colleges").autocomplete({
source: "<?php echo base_url('index.php/'); ?>test/lookup",
minLength:2,
select: function(event, ui) {
var x = ui.item.value;
x = encodeURIComponent(x);
c = x.replace(/%20/g, "-");
location.href = "index.php/test/college/"+c;
return false;
}
});
});
</script>
我创建了自动填充建议框,其中我的字符串在网址中
Akshaya%20Institute%20of%20Technology%2C%20Tumkur%20(engineering)
那么,如何使用Javascript将%20
和%2C
替换为来自字符串的符号?请帮帮我。
谢谢
答案 0 :(得分:1)
您可以将转义字符\
用于%20
和%2C
,
var x = 'Akshaya%20Institute%20of%20Technology%2C%20Tumkur%20(engineering)';
var c = x.replace(/\%20/g, "-").
replace(/\%2C/g, "-");
console.log(c);
&#13;
所以,你的代码就像这样
select: function(event, ui) {
var x = ui.item.value;
x = encodeURIComponent(x);
c = x.replace(/\%20/g, "-").
replace(/\%2C/g, "-");
location.href = "index.php/test/college/"+c;
return false;
}
答案 1 :(得分:0)
请使用以下代码将%20和%2C替换为 - symbol,我在下面的代码中进行了测试。它正在发挥作用。
<script type="text/javascript">
$(this).ready( function() {
$("#colleges").autocomplete({
source: "<?php echo base_url('index.php/'); ?>test/lookup",
minLength:2,
select: function(event, ui) {
var x = ui.item.value;
x = encodeURIComponent(x);
c = x.replace(/%20/g, "-").replace(/%2C/g, "-");
location.href = "index.php/test/college/"+c;
return false;
}
});
});
答案 2 :(得分:-2)
使用javascript decodeURI()
或decodeURIComponent()
功能。