简体   繁体   English

使用jQuery更改文本输入值-Chrome扩展程序

[英]Change text input value with jQuery - Chrome extension

I am trying to change input value in particular web site from chrome extension. 我正在尝试从chrome扩展名更改特定网站的输入值。 In order to do that I am using jQuery in my content script. 为此,我在内容脚本中使用了jQuery。 It works in most of the cases, but I didn't manage to change value of the input when it is part of AngularJS view. 它在大多数情况下都有效,但是当它是AngularJS视图的一部分时,我没有设法更改输入的值。 I found the same problem when I use let say kendoUI. 当我说kendoUI时,我发现了同样的问题。 I am trying to set the value calling $('element').val('value') and then try to call blur and change event, but without any success. 我试图设置调用$('element').val('value') ,然后尝试调用模糊和更改事件,但是没有成功。 I went through may be 99% of the posts related to this topic, but still can't find working solution. 我浏览的可能是与该主题相关的帖子的99%,但仍然找不到有效的解决方案。

You just need to Call $scope.apply() in order to let angular know about updating the bindings. 您只需要调用$ scope.apply()即可让角度了解有关更新绑定的信息。 This is mainly because by default, angular doesn't know anything about the changes you are making in jQuery. 这主要是因为默认情况下,angular对您在jQuery中所做的更改一无所知。

Below is a sample code, and here is a jsFiddle . 下面是示例代码,这是jsFiddle Hope this helps. 希望这可以帮助。

app.controller('testCtrl', ['$scope', function ($scope) {

    $scope.changeValue = function() {
     $('#test').val("new Value");
        $scope.apply();
    }

  <div class="form-inline">
            <input type="text" id="test" value="test">
            <input type="submit" value="submit" ng-click="changeValue()">
  </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM