文本框Javascript中的替代案例

时间:2017-02-08 05:48:59

标签: javascript jquery input textbox

我想制作一段替代用户输入文本的代码。目前,我的代码如下所示:

var num;

function toTitleCase(str) {
    return str.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
}

$('input, textarea').onkeyup(function () {
    prev = true;
    for (num = 0; num < this.length; num += 2)
    {
        this.substr(num).val(toTitleCase(this.substr(num)));
    }
});

问题在于它不起作用(如同没有任何变化)。我尝试将其设为Chrome扩展程序,但它并没有改变案例。这是我测试代码有点奇怪或代码有错误的方法吗?

2 个答案:

答案 0 :(得分:0)

试试这个。简单的代码。 Codepen Link

$('input, textarea').keyup(function () {
 var value = $(this).val();
 var altText = '';
 for (num = 0; num < value.length; num ++)
 {
    if(num%2==0)
      altText += value[num].toUpperCase();
    else
      altText += value[num].toLowerCase();
 }
 $(this).val(altText);
});

答案 1 :(得分:0)

var app = angular.module("musicApp", ["ngRoute"]);
app.config(function($routeProvider) {
  $routeProvider.when("/Items", {
      templateUrl: "view-list.html",
      controller: "listController"
    })
    .when("/Items/add", {
      templateUrl: "view-detail.html",
      controller: "addController"
    })
    .when("/Items/:index", {
      templateUrl: "view-detail.html",
      controller: "editController"
    })
    .otherwise({
      redirectTo: "/Items"
    });
});


app.factory("productService", ["$rootScope", function($rootScope){
  var service={};
  var data = [{
      name: "Artist1",
      genre: "Genre1",
      rating: "Rating1"
    }, {
      name: "Artist2",
      genre: "Genre2",
      rating: "Rating2"
    }];

    service.getProducts = function(){};
    service.addProduct = function(product){};
    service.editProduct = function(product){};
  return service;
}]);
app.controller("listController", ["$scope", "$location", "$routeParams",
  function($scope, $location, $routeParams) {

    $scope

    $scope.addProduct = function() {
      $location.path("/Items/add");
    };

    $scope.editItem = function(index) {
      $location.path("/Items/" + index);
    };

  }
]);

app.controller("addController", ["$scope", "$location", "$routeParams",
  function($scope, $location, $routeParams) {
    $scope.save = function() {
      $location.url("/Items");
    };
    $scope.cancel = function() {
      $location.path("/Items");
    };
  }
]);


app.controller("editController", ["$scope", "$location", "$routeParams",
  function($scope, $location, $routeParams) {
    $scope.save = function() {
      $location.path("/Items");
    };
    $scope.cancel = function() {
      $location.path("/Items");
    };
  }
]);