jQuery無双

jQueryをとことん使いこなす! jQuery1.12対応。使い方、入門、サンプル、ノウハウをとことん極めた最強ブログ。

val():要素のvalueの値を取得する

      2016/04/19

要素のvalueの値を取得するの書式

$("要素名").val();

要素のvalueの値を取得する場合は、
$("要素名").val();を記述します。
valメソッドを使用します。

 

具体例(要素のvalueの値を取得するのサンプルコード)

サンプルでは以下の事をしてます。

1.valメソッドを使って、textフィールドidがidTestText2のvalue値の取得。

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
<!--
body {background-color: #ffffff;}
-->
.classAttention { color: #ff0000; font-weight: bold; }
</style>
<script src="../../js/jquery-1.12.2.min.js"></script>
<script>
$(function(){
  var result;
  result = $("input:text#idTestText2").val();
  $("#resultDiv").text('textフィールドidがidTestText2のvalue値は' + result);
});
</script>
</head>
<body>
  <input id = "idTestText1" type = "text" value = "テキストの内容1"><br>
  <input id = "idTestText2" type = "text" value = "テキストの内容2"><br>
  <input id = "idTestText3" type = "text" value = "テキストの内容3"><br>
  <div id = "resultDiv"></div>
</body>
</html>
 
要素のvalueの値を取得するのサンプルコードの結果

jquery,val,value,取得

 

 - 属性(attributes) , , ,