为什么我的 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();
}
});