How can i combine words from input?
For example if i have form like this:
<form >
<input type="text" name="name" id="my_input开发者_JAVA技巧">
<input id="search_tags" type="submit" value="Search">
</form>
And i wrote words 'example' and 'item' value of #my_input'
will be 'example item'. How can i build from written words something like this: '/example-item/'
?
var combinedWord = "/" + $("#my_input").val().replace(/ /g, "-") + "/";
Use a regex.. maybe something like value.replace(/[^\w\d]+/gi, '-'); It'll replace anything that isn't a number or letter.. which looks like what you might want to be doing.
var value = "example item 2";
value.replace(/[^\w\d]+/gi, '-');
console.log(value) // will be example-item-2
value = '/' + value + '/';
console.log(value) // will be '/example-item-2/'
if you want to do it for each of your input then
var combinedWord;
$("input[type=text]").each(function()
{
combinedWord+=$(this).val(); //if space between different input is to be maintained then add .replace(' ', '-') here
});
return "/" + combinedWord + "/"; //if all spaces need to be replaced add .replace(' ', '-') here.
Replace all spaces with a hyphen:
var my_input_val = $('#my_input').val().replace(/\s/g, '-');
And then concatenate the forward slashes onto either end:
var concat_my_input = '/' + my_input_val + '/';
.replace()
is a very flexible function that can either match with a regular expression or a string. See the w3chools definition for more information.
精彩评论