下面的代码不起作用?
function titleCase(str){
var newStr = str.split(" "); //split string turn it into seperated words[]
var resutl;
for(vari=0; i < newStr.length; i++){ //iterate all words
var result = newStr[i].charAt(0).toUpperCase +
// find first letter and turn it into capital
newStr[i].subString(1).toLowerCase();
}
return result.join(" ");
}
答案 0 :(得分:0)
导致您的代码是字符串,而不是数组。你不能加入一个字符串。
循环的每次迭代都是用新单词替换变量结果。你需要初始化一个结果数组[]并将每个结果推送到数组,然后在循环完成后加入数组。
答案 1 :(得分:0)
在空格上分解字符串并使用以下函数迭代它:
<html lang="en" ng-app="courseCatalog" ng-controller="resultsController">
<!-- some head info -->
<body>
<form id="search_form" name="search_form" ng-submit="searchNow()">
<!--
form fields in here, for example:
-->
<input type="text" id="txtkeywords" name="keywords" ng-model="search.keywords">
<input type="text" id="txtsubject" name="term" ng-model="search.subject">
<input type="text" id="textterm" name="subject" ng-model="search.term">
<input id="submit-button" name="submit-button" type="submit"
value=" Search Now "/>
</form>
<!-- Here is the , I pre-populate some form fields -->
<div id='filter_holder' class='hidden_box'>
<div class='hidden_box'>{{search.term='1|201601';}}</div>
<div class='hidden_box'>{{search.subject='6|BIO';}}</div>
</div>
<!-- And here, I try to manually submit the form -->
<script type="text/javascript">
document.getElementById('submit-button').click();
</script>
</body>
答案 2 :(得分:0)
result
需要是一个数组,而且您的代码中也有一些拼写错误,例如:缺少()
function titleCase(str) {
var newStr = str.split(" ");
var result = [];
for (var i = 0; i < newStr.length; i++) {
result.push(newStr[i].charAt(0).toUpperCase() + newStr[i].substring(1).toLowerCase());
}
return result.join(' ');
}
var str = 'hELLO wORLD';
document.write(titleCase(str));
&#13;
答案 3 :(得分:0)
你可以试试这个:
<!DOCTYPE html>
<html>
<body>
<?php
error_reporting( E_ALL & ~E_NOTICE );
// define variables and set to empty values
$name = $email = $bday = $gender = $image="";
//$image=$_FILES["image"]["name"];
if(isset($_POST['submit'])){
$name = $_POST["name"];
$email = $_POST["email"];
$bday=$_POST["bday"];
$gender = $_POST["gender"];
$image =$_POST["image"];
$servername = "localhost";
$username = "mvs1";
$password = "";
$dbname="scootsy_categories";
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
$sql = "INSERT INTO user_details(name,email,bday,gender,image) VALUES ('$name','$email','$bday','$gender','$image')";
if ($conn->query($sql) === TRUE) {
// echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
//if($name !="" || $email !="" || $bday !="" || $gender !="" || $image !="" )
//{
// $sql="DELETE FROM user_details where name='$name' || email='$email' || bday='$bday' || gender='$gender' || image='$image'";
//}
$conn->close();
}
echo "Successful";
?>
</body>
</html>
另一种方法:
function titleCase(str){
var newStr = str.split(" ");
var result = [];
for(var i=0; i < newStr.length; i++){
result.push(newStr[i].charAt(0).toUpperCase() +
newStr[i].substring(1).toLowerCase());
}
return result.join(' ');
}
答案 4 :(得分:0)
尝试使用正则表达式
var data = "The mission is to turn each word's first letter into capital";
data = data.replace(/ (.)/g,function(w){return w.toUpperCase()});
缺点:这不会使第一个字符大写。