jquery获取文本框的内容

使用jquery获取文本框的内容有以下几种:

1.根据ID取值(id属性):

// javascript 
<script type="text/javascript">
   function getUserName(){
        var username= $("#username").val(); 
  }
</script>
// html 
<div><input type="text" id="username"></div>
<div><input type="button" value="commit" onclick="getUserName()"></div>

2.根据类取值(class属性)

// javascript 
<script type="text/javascript">
   function getUserName(){
        var username= $(".username").val(); 
  }
</script>
// html 
<div><input type="text" class="username"></div>
<div><input type="button" value="commit" onclick="getUserName()"></div>

3. 根据name取值,该方法也可以取其他的属性

// javascript 
<script type="text/javascript">
   function getUserName(){
        var username= $("input[name='username']").val(); 
  }
</script>
// html 
<div><input type="text" name="username"></div>
<div><input type="button" value="commit" onclick="getUserName()"></div>
// javascript 
<script type="text/javascript">
   function getUserName(){
        var username= $("input[id='username']").val(); 
  }
</script>
// html 
<div><input type="text" id="username"></div>
<div><input type="button" value="commit" onclick="getUserName()"></div>

方法是很多的,灵活运用就好。

原文地址:https://www.cnblogs.com/30go/p/5704274.html