开发者

How can i convert this statement to Jquery Completely

开发者 https://www.devze.com 2023-01-23 19:26 出处:网络
if(document.getElementById(\'txt1\') != null){ $(\"#txt1\").val(document.getElementById(\'txt1\').value.toUpperCase());
     if(document.getElementById('txt1') != null){
    $("#txt1").val(document.getElementById('txt1').value.toUpperCase());        
}

How can i convert this statement to Jquery开发者_如何学C Completely


You can use a function with .val() like this:

$("#txt1").val(function(i, val) { return val.toUpperCase(); });

...and it it doesn't find an id="txt1" element it just won't run on any, that's the way jQuery chains work, so your if() check is taken care of as well.


or like this

var $txt = $("#txt");
if($txt.length){
  $txt.val($txt.val().toUpperCase());
}
0

精彩评论

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