【发布时间】:2012-12-16 18:11:43
【问题描述】:
为什么我的 jquery 没有用 '-' 替换所有空格。它只用'-'替换第一个空格
$('.modhForm').submit(function(event) {
var $this = $(this),
action = $this.attr('action'),
query = $this.find('.topsearchbar').val(); // Use val() instead of attr('value').
if (action.length >= 2 && query.length >= 2 && query.lenght <=24) {
// Use URI encoding
var newAction = (action + '/' + query.replace(' ','-'));
console.log('OK', newAction); // DEBUG
// Change action attribute
$this.attr('action', newAction);
} else {
console.log('To small to be any good'); // DEBUG
// Do not submit the form
event.preventDefault();
}
});
【问题讨论】:
-
String.replace -- 请参阅本页的第二个示例。
标签: javascript jquery replace