Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angularjs,修改/删除表单提交上的数据_Javascript_Angularjs - Fatal编程技术网

Javascript Angularjs,修改/删除表单提交上的数据

Javascript Angularjs,修改/删除表单提交上的数据,javascript,angularjs,Javascript,Angularjs,我不想发送表单中的输入字段。即使我删除了输入字段上的名称,它仍然会被发送,可能是因为角度魔法 为了防止这种情况,我想如果我可以从post请求中删除此项,这将是解决方案 <input type='radio' ng-model='birthday' ng-value='true'> 表单提交时,尽管输入没有name属性,但帖子的字段名为birthday。那么我如何防止它出现呢 表单是html模板,在ng submit时调用controller我想您可能正在查找disabled属性

我不想发送表单中的输入字段。即使我删除了输入字段上的名称,它仍然会被发送,可能是因为角度魔法

为了防止这种情况,我想如果我可以从post请求中删除此项,这将是解决方案

<input type='radio' ng-model='birthday' ng-value='true'>

表单提交时,尽管输入没有name属性,但帖子的字段名为birthday。那么我如何防止它出现呢


表单是html模板,在ng submit时调用controller

我想您可能正在查找
disabled
属性:

<input type='radio' ng-model='birthday' ng-value='true' disabled="true">


已编辑

以下示例说明了如何使用disabled属性在提交表单时不发送不需要的信息:

<html>

    <body>

        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

        <script>

            var TestApp = angular.module("TestApp", []);        

            TestApp.controller('TestCtrl', function($scope) {

              $scope.needsBirthdayDisabled = false;
              $scope.needsBirthday = false;

              $scope.sendForm = function(form) {        

                $scope.needsBirthdayDisabled = true; //if you comment this line, the "yep"'s input value will be sent with the form

                form.$submitted = true;      

              };

            });

        </script>

        <div ng-app="TestApp" ng-controller="TestCtrl">

            <!-- the method is set to GET for test purpose (so we can easily see the sent values on the URL) -->
            <form action="" method="GET" name="myForm" ng-submit="sendForm(this)">

                <div>
                    <label for="name">Name</label>
                    <input type="text" name="name" ng-model="name"/> 
                </div>

                <div ng-show="needsBirthday">
                    <label for="birthday">Birthday</label>
                    <input type="text" name="birthday" ng-model="birthday"/> 
                </div>

                <div>
                    <label>Needs Birthday</label>               
                    Yep <input type='radio' name="yep" ng-model='needsBirthday' ng-value='true' ng-disabled="needsBirthdayDisabled">                                    
                </div>

                <input type="submit" value="Go!"/>

            </form>

        </div>

    </body>

</html>

var TestApp=angular.module(“TestApp”,[]);
TestApp.controller('TestCtrl',函数($scope){
$scope.needsBirthdayDisabled=false;
$scope.needsBirthday=false;
$scope.sendForm=函数(形式){
$scope.needsBirthdayDisabled=true;//如果对此行进行注释,“yesp”的输入值将与表单一起发送
表格:$submitted=true;
};
});
名称
生日
需要生日
是的

这不是问题。提供一些代码。你试了什么?你是如何建立这个表单的?您是如何将其链接到控制器的?如何使用Ajax发送表单?再次使用信息更新,提供一些代码:如果您不提供发送数据的函数,我们怎么能想象,发生了什么?添加发送数据的函数、发送的数据以及数据的来源。通常,所有表单数据都有一个对象,并将其链接到例如
ng model=“form.birth”
。在控制器中,您可以使用Ajax将
$scope.form
对象发送到后端。我不能使用它,因为我希望启用它,因为它用于显示或隐藏部分表单,但不包含后端需要的任何数据。您可以在提交表单之前将此属性设置为
true
,然后将其值返回到
false
(您可以使用范围属性)如果运行验证,它们会阻止表单提交,但值已设置为true。我已经编辑了我的答案,为您提供了一个如何执行的示例。我已经对它进行了测试,它可以正常工作(阻止表单提交没有问题)