jQuery

jQuery val() メソッドによる値の取得と変更

2014年5月15日

val() メソッドはエレメントの value 属性を取得、変更します。

<head>
    <script type="text/javascript">
        $(function () {
            // ページ初期化時に値を設定
            $("#text1").val("default text");
        });

        function getVal()
        {
            // 値の取得
            var currentVal = $("#text1").val();
            alert(currentVal);
        }

        function changeVal()
        {
            // 値の変更
            $("#text1").val("new value");
        }
    </script>
</head>
<body>
    <input type="text" id="text1" />
    <br />
    <button onclick="getVal()">get value</button>
    <br />
    <button onclick="changeVal()">change value</button>
</body>

-jQuery