开发者

copying one field to another but adding commas between words?

I have a field called title.

I have a second field called tagline.

The user will fill in the title manually, and I would need the tagline field to be a copy of the title field, but with commas between each word.

So, a user would put "red apples" in the title, then the tagline would be the same as the title but with commas between the words.

Title = red apples

tagline = red, apples

Can somebody t开发者_Go百科alk me through the code to do this please?

Thanks


var title = "red apples";
var tagline = title.split(" ").join(", ");


$title = 'red apples '; 
$title= trim($title); //make sure no space before or after the string, it would convert to comma
$tagline = str_replace(' ', ',', $title)


Here is a working example

<input id="title" type="text" value="Red Apples" />
<input id="tagline" type="text" value="" />
<input type="button" value="do stuff" onClick="stuff()"/>

<script type="text/javascript">
    window.stuff = function(){
        title = document.getElementById("title");
        tagline = document.getElementById("tagline");
        tagline.value = title.value.split(" ").join(", ");
    }
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜