javascript 如何在此表单中添加 ucwords 和 strtolower

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4609558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:17:20  来源:igfitidea点击:

How to add ucwords and strtolower to this form

javascript

提问by lisovaccaro

I want "s.value" to be turned all to lowercase and then ucwords to it but I don't know how to do it since it's inside a form.

我希望将“s.value”全部转为小写,然后将 ucwords 转为它,但我不知道该怎么做,因为它在表单中。

Basically I want to do something like this: ucwords(strtolower( s.value here ));

基本上我想做这样的事情: ucwords(strtolower( s.value here ));

This is the form:

这是表格:

   <form role="search" method="get" id="searchform" action="http://chusmix.com/?s=" onsubmit="if (document.getElementById('s2').value.length > 5) window.location = action + '<php echo $city; ?>++++' + s.value; return false;" >

Thanks

谢谢

回答by Saul

<script type="text/javascript">

function ucwords (str) {
    return (str + '').replace(/^([a-z])|\s+([a-z])/g, function () {
        return .toUpperCase();
    });
}

function strtolower (str) {
    return (str+'').toLowerCase();
}

</script>

<form role="search" method="get" id="searchform" action="http://chusmix.com/?s="  onsubmit="if (document.getElementById('s2').value.length > 5) window.location = action + '<?php echo $city; ?>++++' + ucwords(strtolower(s.value)); return false;" >

The javascript functions are from PHP.JS

javascript 函数来自PHP.JS

回答by Chandu

Javascript String have two functions: toLowerCase() and toUpperCase, you can use them with

Javascript String 有两个函数:toLowerCase() 和 toUpperCase,你可以将它们与

function toInitCap(input)
{
 input = input || "";
 return input.substring(0,1).toUpperCase() + input.substring(1,str.length).toLowerCase(); 
}

toInitCap(s.value)

回答by Martin Lyne

You may, like me, want something that does this on one line, I used:

你可能和我一样,想要在一行上做这件事,我用过:

"your strING".toLowerCase().replace(/(?:(^.{1})|\ [a-z]{1})/g, function(a){return a.toUpperCase();})

returns Your String

回报 Your String

回答by Martin Lyne

+ <?php echo ucwords(strtolower($s_value)); ?>;

回答by Frederic Fillon

str.length => input.length

str.length => input.length

function toInitCap(input)
{
 input = input || "";
 return input.substring(0,1).toUpperCase() + input.substring(1,input.length).toLowerCase(); 
}

toInitCap(s.value)

回答by Colum

You want to do:

你想做:

$out = ucwords(strtolower($_GET['s.value']));

If s.value is the input name you want to pass

如果 s.value 是您要传递的输入名称

回答by zamN

If you want to do it while they are editing the text within the text box then I'm not sure what you would do but if you want to make it lowercase then uppercase AFTER they submit the form then its simple.

如果您想在他们编辑文本框中的文本时执行此操作,那么我不确定您会做什么,但是如果您想在他们提交表单后将其设为小写然后大写,则它很简单。

ucwords(strtolower($_GET['formid']))

ucwords(strtolower($_GET['formid']))