Jquery-attr-val

提供:Dev Guides
移動先:案内検索

jQuery-val()メソッド

説明

  • val()*メソッドは、最初に一致した要素の入力値を取得します。

構文

このメソッドを使用するための簡単な構文は次のとおりです-

selector.val( )

パラメーター

これは、この方法で使用されるすべてのパラメータの説明です-

  • NA

次の例では、2番目の段落の最初の入力ボックスの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>

これにより、次の結果が生成されます–