实例 jQuery 使用函数设置 value 属性的值

x
 
<!DOCTYPE html>
<html>
<head>
<script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("input:text").val(function(n, c){
      return c + " Griffin";
    });
  });
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user" value="Peter"></p>
<button>Set the value of the input field</button>
</body>
</html>
                    

输出结果