Javascript 正在修复URl,以便没有空格
我有一个通过jquery动态生成其位置的按钮:Javascript 正在修复URl,以便没有空格,javascript,jquery,html,Javascript,Jquery,Html,我有一个通过jquery动态生成其位置的按钮: <a id="final_location" href="/pre_config/step4" class="proceed-btn right">Proceed To Next Step >>></a> 这非常有效,但当类型为\u的\u站点中的html是两个单词时,问题就出现了。。。我得到这个网址: pre_config/step4/Pizza Delivery/
<a id="final_location"
href="/pre_config/step4"
class="proceed-btn right">Proceed To Next Step >>></a>
这非常有效,但当类型为\u的\u站点中的html是两个单词时,问题就出现了。。。我得到这个网址:
pre_config/step4/Pizza Delivery/2
有没有办法让url只给我这样的第一个词:
pre_config/step4/Pizza/2
也许可以更改为只返回第一个单词?使用
encodeURIComponent($('#type_of_station').text().match(/^\S*/)[0])
而不是escape($('#键入_of_station').html())
来获取第一个单词(用空格分隔) 使用
encodeURIComponent($('#type_of_station').text().match(/^\S*/)[0])
而不是
escape($('#键入_of_station').html())
来获取第一个单词(用空格分隔) 这应该是短语中的第一个单词:
var type = $('#type_of_station').html();
var i = type.indexOf(' ');
if (i != -1) type = type.substr(0, i);
这应该是短语中的第一个单词:
var type = $('#type_of_station').html();
var i = type.indexOf(' ');
if (i != -1) type = type.substr(0, i);
你可以做:
var name = "Pizza Delivery";
var pieces = name.split(" ");
alert(pieces[0]); // Pizza
或者干脆做:
var name = "Pizza Delivery";
name = name.replace(/\s/g, "-", name); // feel free to change the - delimiter
alert(name); // Pizza-Delivery
你可以做:
var name = "Pizza Delivery";
var pieces = name.split(" ");
alert(pieces[0]); // Pizza
或者干脆做:
var name = "Pizza Delivery";
name = name.replace(/\s/g, "-", name); // feel free to change the - delimiter
alert(name); // Pizza-Delivery