使用AngularJS在视图中预先填写并提交表格
Pre fill and submit a form in in a view with AngularJS
我有一个非常简化的AangularJS应用程序。它运行得很好。
现在,我需要填写一些字段,并直接从视图提交表单,而无需用户交互。
这是Javascript代码(再次简化,我删除了许多其他元素)
mainApp.controller("resultsController",FormController);
function FormController($scope,APIService) {
$scope.result ="";
$scope.search = { keywords: "",
result:1,
term: "-1",
subject: "-1"};
$scope.searchNow = function() {
APIService.getTheResults($scope);
} // $scope.searchNow
}
function getResults($http) {
var results = {};
url = "...";
results.searchResults = function() {
var lurl = url + "a=s";
return $http( {method: "JSONP", url:lurl} );
} // results.searchResults
results.getTheResults = function(scope) {
var lurl = url;
$http({
method: 'POST',
url:lurl,
data:jQuery.param(scope.search),
headers:{'Content-Type':'application/x-www-form-urlencoded'}})
.success(function(lresponse){
var theResponse;
theResponse = eval(lresponse);
scope.mydata = theResponse.data[0]
})
.error( function(data,status) {
scope.result = status;
});
} // results.getTheResults
return results;
} // function getResults($http)
这是HTML视图:
<html lang="en" ng-app="courseCatalog" ng-controller="resultsController">
<!-- some head info -->
<body>
<form id="search_form" name="search_form" ng-submit="searchNow()">
<!--
form fields in here, for example:
-->
<input type="text" id="txtkeywords" name="keywords" ng-model="search.keywords">
<input type="text" id="txtsubject" name="term" ng-model="search.subject">
<input type="text" id="textterm" name="subject" ng-model="search.term">
<input id="submit-button" name="submit-button" type="submit"
value=" Search Now "/>
</form>
<!-- Here is the , I pre-populate some form fields -->
<div id='filter_holder' class='hidden_box'>
<div class='hidden_box'>{{search.term='1|201601';}}</div>
<div class='hidden_box'>{{search.subject='6|BIO';}}</div>
</div>
<!-- And here, I try to manually submit the form -->
<script type="text/javascript">
document.getElementById('submit-button').click();
</script>
</body>
但我只能用url中的所有表单字段重新加载页面:
url…/?txtterm=-1&txtsobject=-1&提交按钮=+搜索+现在+
我也尝试了提交文件形式提交,但我没有得到任何结果。
如果我删除所有预填写/提交并手动填写和发送表格,我会得到正确的结果。
你知道如何从视图中提交表格吗?
尝试使用表单方法post。因此,将您的表单标签更改为:
<form id="search_form" name="search_form" ng-submit="searchNow()" method="post">
如w3schools本章所述,当使用post方法时,数据将不会显示在url中。
"POST提供了更好的安全性,因为提交的数据不可见在页面地址中。"
已解决:我注意到页面试图快速提交表单。首先,我将计时器设置为表单加载后5秒,然后调用提交按钮的点击功能。然后,我只使用了一个window.unload函数来在创建整个DOM时调用:
window.onload = function(){ document.getElementById('submit-button').click(); };
这很好。我希望这能帮助其他人。
谢谢!
相关文章:
- 是否可以使用动作=“;someFunction()"提交表格时
- 在提交过程中使用同步确认灯箱提交表格
- 在提交未提交表格时替换Div
- 返回假后提交表格
- NG模型在提交表格时未注册
- 提交表格前检查条款和条件
- 可以'不要在express/angular应用程序上提交表格
- return false dos't禁止提交表格
- 使用Greasemonkey提交表格
- 使用AngularJS在视图中预先填写并提交表格
- 在提交表格之前,请检查是否填写了重述2
- 提交表格后留下的表格
- 避免在按下“”时提交表格;输入“;按钮
- 在提交表格时IE8赢得了't将来自输入的属性以外的属性添加到请求中
- 为什么这个代码显示异常行为?(未提交表格)
- 为什么这不足以防止重复提交表格
- 点击链接提交表格并发送到多个电子邮件 ID
- Phonegap Camera API (iOS) 提交表格
- 验证通过后提交表格
- 提交表格后在同一页面上统计