我为一个系统写一个JS脚本,最后我把值传给input的value,form不能成功submit,我感觉是form验证没有被触发
前台代码:
<form class="ng-pristine ng-invalid ng-invalid-required" advance-on-enter="" autocomplete="off" name="destroyForm" ng-submit="processDestroy()" novalidate=""> <div class="form-group"> <label>Ticket Number</label> <input class="ng-isolate-scope ng-pristine ng-invalid ng-invalid-required" name="ticket" ng-model="destroyData.ticket" remote-validate="validateTicket" required="" type="text"> <span class="alert alert-error plain ng-hide" ng-show="destroyForm.ticket.$invalid && destroyData.ticket.length > 0">Invalid Ticket Number</span> </div> <div class="form-group"> <label>Destruction Machine Tag</label> <input class="ng-isolate-scope ng-pristine ng-invalid ng-invalid-required" autofocus="autofocus" name="bin_asset_tag" ng-model="destroyData.bin_asset_tag" remote-validate="validateBin" required="" type="text"> <span class="alert alert-error plain ng-hide" ng-show="destroyForm.bin_asset_tag.$invalid && destroyData.bin_asset_tag.length > 0">Invalid Asset Tag</span> </div> <div class="form-group"> <label>Drive Serial Number</label> <input class="ng-pristine ng-invalid ng-invalid-required" name="serial_id" ng-model="destroyData.serial_id" required="" type="text"> </div> <div class="form-horizontal"> <input disabled="disabled" class="btn btn-primary btn-lg" ng-disabled="destroyForm.$invalid" type="submit"> </form>
JS脚本:
var e = document.createElement('input'); e.type = 'button'; e.value = 'Start'; e.setAttribute('id', 'dbtn'); e.setAttribute('name', 'dbtn'); e.className = 'btn btn-primary btn-lg'; e.onclick = function startdestroy() { var serial_text = document.getElementsByName('serial_id')[1]; //alert(serial_id); serial_text.value = “12345”; var sbtn=document.getElementsByClassName('btn btn-primary btn-lg') [1]; sbtn.removeAttribute("disabled"); //serial_text.removeAttribute("required") document.getElementsByClassName('btn btn-primary btn-lg') [1].click(); } }); }; var inputdiv = document.getElementsByClassName('form-horizontal') [1]; inputdiv.appendChild(e);
通过在输入框手动输入和复制粘贴可以验证,直接赋值就不能触发验证,有什么办法让验证呢?
简单的说,在输入框手输和复制可以触发验证,而采用JS脚本直接为其复制,则不能触发验证。
我没搞懂,为嘛在angular里面,你还在用onclick,和操作dom?
@孩纸有点硬: 通过$('xxx').scope().xxx=111 来赋值。
angularjs 值是绑定在ng-model上的 如果要赋值也是 $scope.赋值对象=值
@孩纸有点硬: 不需要操作dom的, 你js写的脚本是想实现什么功能的?
@孩纸有点硬: 你如果使用的是angularjs的路由的话,可以在controller 中 使用用 $scope.destroyData.serial_id= $routeParams.接收的对象
页面中
<input class="ng-pristine ng-invalid ng-invalid-required" name="serial_id" ng-model="destroyData.serial_id" required="" type="text">