如何从url获取字符串,将其分配给输入文本类型并运行表单

时间:2014-06-10 19:09:11

标签: javascript

我可以通过googlechrome控制台正常运行,而无需重新加载页面

function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
jQuery('#addressInput').val(getParameterByName('addressInput'));
jQuery('#addressSubmit').click();

然而,在我使用这种结构上传我的.js文件后,我无法运行它。

jQuery(document).ready(function() {
getParameterByName(name);
jQuery('#addressInput').val(getParameterByName('addressInput'));
jQuery('#addressSubmit').click();

}

function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

我收到此错误

Uncaught SyntaxError: Unexpected token function 

任何想法如何才能让它正确运行?

提前致谢

1 个答案:

答案 0 :(得分:1)

jQuery(document).ready(function() {
jQuery('#addressInput').val(getParameterByName('addressInput'));
jQuery('#addressSubmit').click();

});  // <-- close the bracket

function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}