jQuery 中的 val( )方法函数

首页 / jQuery入门教程 / jQuery 中的 val( )方法函数

val()方法获取第一个匹配元素的输入值。

val( ) - 语法

selector.val( ) 

val( ) - 示例

以下示例将在第二段中设置第一个输入框的HTML内容-

<html>
   <head>
      <title>The Selecter Example</title>
      <script type="text/javascript" 
         src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
   
      <script type="text/javascript" language="javascript">
         $(document).ready(function() {
            var content=$("input").val();
            $("p#pid2").text(content);
         });
      </script>
      
      <style>
         .red { color:red; }
         .green { color:green; }
      </style>
   </head>
   
   <body>
      <input type="text" value="First Input Box"/>
      <input type="text" value="Second Input Box"/>
      <p class="green" id="pid1">This is <i>first paragraph</i>.</p>
      <p class="red" id="pid2">This is second paragraph.</p>
   </body>
</html>

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/jquery/attr-val.html

来源:LearnFk无涯教程网

 

This is first paragraph.

First Input Box

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

朱赟的技术管理课 -〔朱赟〕

手机摄影 -〔@随你们去〕

成为AI产品经理 -〔刘海丰〕

Spring编程常见错误50例 -〔傅健〕

Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕

去无方向的信 -〔小麥〕

人人都用得上的数字化思维课 -〔付晓岩〕

Vue 3 企业级项目实战课 -〔杨文坚〕

大型Android系统重构实战 -〔黄俊彬〕

好记忆不如烂笔头。留下您的足迹吧 :)