如何在没有用户单击提交的情况下从表单设置JavaScript变量?

时间:2011-09-01 00:29:28

标签: javascript ajax forms variables input

我有一个包含多个不同文本输入的表单。每个值对应一个唯一的JavaScript变量。然后在页面上写出变量。

有没有办法自动更新变量,这样用户每次都不必点击提交并重新加载页面?我听说AJAX可能是一个潜在的答案,但我不熟悉它。如果该路由最好,是否会推荐某些脚本?

1 个答案:

答案 0 :(得分:0)

您可能希望将jQuery视为浏览器中javascript的良好开端。例如,你可以写:

<form id="form-id">
    <input id="first" type="text" />
    <input id="second" type="text" />
    <input id="third" type="text" />
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
var first, second, third;
$(function(){
    $("#form-id").submit(function(){
        first = $("#first").value();
        second = $("#second").value();
        third = $("#third").value();
        return false; //Prevent the form from submitting.
    });
});
</script>

http://jquery.com/