从url填充html表单并自动提交

时间:2014-02-15 00:07:50

标签: javascript

我正在使用以下代码通过网址填充表单,但是可以将其修改为自动提交表单吗?

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
    // grab the entire query string
    var query = document.location.search.replace('?', '');

    // extract each field/value pair
    query = query.split('&');

    // run through each pair
    for (var i = 0; i < query.length; i++) {
        // split up the field/value pair into an array
        var field = query[i].split("=");

        // target the field and assign its value
        $("input[name='" + field[0] + "'], select[name='" + field[0] + "']").val(field[1]);
    }
});
</script>
</head>

1 个答案:

答案 0 :(得分:0)

您只需将此行添加到脚本的底部,而是使用表单的ID。

$("#MyFormId").submit();
相关问题