jQuery:几个单词的首字母大写,其余单词小写

时间:2018-11-06 05:16:18

标签: jquery wordpress

我在WordPress网站上有以下代码:

jQuery(document).ready(function($) {
    $.fn.capitalize = function () {
        var wordsToIgnore = ["adalah","agar","akibat","akan","ah","ala","alias","antara","apabila"],
        minLength = 2;

        function getWords(str) {
            return str.match(/\S+\s*/g);
        }

        this.each(function () {
            var words = getWords(this.value);
            $.each(words, function (i, word) {
            if (wordsToIgnore.indexOf($.trim(word)) == -1 && $.trim(word).length > minLength) {
                words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
            }
            });
            this.value = words.join("");
        });
        };

    document.body.onkeyup = function(e){
        if(e.which == 32){
            $('#fp_title').capitalize();
        }
    }
});

代码工作得很好。除“ WordToIgnore”以外的任何单词的首字母都将大写。但是我也需要将“ WordToIgnore”的首字母小写的函数。因此,当用户在字段(#fp_title)上键入内容时,“ WordToIgnore”的首字母将自动小写,其余单词的首字母将大写。我该怎么办?

示例:

(“ adalah ”是WordToIgnore)

  • “ cara adalah sesuatu”应为“ Cara adalah Sesuatu”
  • “ Cara Adalah Sesuatu”应为“ Cara adalah Sesuatu”
  • “ Cara Adalah sesuatu”应为“ Cara adalah Sesuatu”

结果应该相同。

英语:

(“ ”是WordToIgnore)

  • “我或你”应为“我或你”
  • “我还是你”应该是“我还是你”
  • “我还是你”应该是“我还是你”

1 个答案:

答案 0 :(得分:0)

在if条件中进行比较之前,也将单词转换为小写,也可以使用else条件,它将按您的要求工作。

<?php

if (isset($_POST['submit'])) {

    $npm_siswa = $_POST['npm_siswa'];
    $nama_siswa = $_POST['nama_siswa'];
    $bidangst_siswa = $_POST['bidangst_siswa'];
    $ipk_siswa = $_POST['ipk_siswa']; 

    $query ="INSERT INTO class(npm_siswa, nama_siswa, bidangst_siswa, ipk_siswa) VALUES('$npm_siswa','$nama_siswa','$bidangst_siswa','$ipk_siswa')";

    $insert_data = mysqli_query($con, $query);
    $goto = header('location: input_data.php');
    if (headers_sent($goto) == true) {
        exit();
    }
}

$checking = "SELECT * FROM class WHERE npm_siswa ='$npm_siswa'";

$process = mysqli_query($con, $checking) or die(mysqli_error());
while($row_filter = mysqli_fetch_assoc($process)) {
    $kelasId = $row_filter['kelasId'];
    $npm_siswa = $row_filter['npm_siswa'];
    $nama_siswa = $row_filter['nama_siswa'];
    $bidangst_siswa = $row_filter['bidangst_siswa'];
    $ipk_siswa = $row_filter['ipk_siswa'];
}

if (mysqli_num_rows($process) > 0) {
    echo "maaf nama anda sudah digunakan"; 
} else {
    $data = "INSERT INTO class(npm_siswa, nama_siswa, bidangst_siswa, ipk_siswa) VALUES('$npm_siswa','$nama_siswa','$bidangst_siswa','$ipk_siswa')";
    $check = mysqli_query($con, $data);
}

?>
jQuery(document).ready(function($) {
    $.fn.capitalize = function () {
        var wordsToIgnore = ["adalah","agar","akibat","akan","ah","ala","alias","antara","apabila"],
        minLength = 2;

        function getWords(str) {
            return str.match(/\S+\s*/g);
        }

        this.each(function () {
            var words = getWords(this.value);
            $.each(words, function (i, word) {
            if (wordsToIgnore.indexOf($.trim(word.toLowerCase())) == -1 && $.trim(word).length > minLength) {
                words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
            }else{
                words[i] = words[i].charAt(0).toLowerCase() + words[i].slice(1);
            }
            });
            this.value = words.join("");
        });
        };

    document.body.onkeyup = function(e){
        if(e.which == 32){
            $('#fp_title').capitalize();
        }
    }
});