动态地向元素添加特定的attr

时间:2016-05-16 10:14:02

标签: javascript jquery angularjs validation angularjs-directive

我使用angular指令创建了valid-file属性,因为文件输入(bootstrap filestyle plugin}没有看到required属性:

    .directive('validFile',function(){
        return {
            require:'ngModel',
            link:function(scope,el,attrs,ngModel){
                //change event is fired when file is selected
                el.bind('change',function(){
                    scope.$apply(function(){
                        ngModel.$setViewValue(el.val());
                        ngModel.$render();
                    })
                })
            }
        }
    })

我需要动态添加或删除valid-file属性。 如果我使用 $("input").attr("valid-file"); 什么都不会改变。 如果我使用$("input").attr("valid-file",true);,则会添加属性valid-file="true",但验证会中断。 你能给我一些建议吗? 这是plunker http://plnkr.co/edit/Gcbb7r05VBTjbh9x5Ma8?p=preview

1 个答案:

答案 0 :(得分:2)

如果要在任何元素中设置任何属性,

<script> $(function() { var id = sessionStorage.getItem('highlightId'); sessionStorage.removeItem('highlightId'); // $('#' + id).css('background-color','yellow'); $('img').mapster( {fillColor: 'ff0000', fillOpacity: 0.5}); $('#' + id).mapster('select'); }); </script> <script src="https://s3.amazonaws.com/clientevents/Image-mapping/jquery.imagemapster.js"></script> 需要2个参数。

离。 .attr() //空也可以。或任何值而不是空

因为只有$("input").attr("valid-file","");用于获取属性值&#34; valid-file&#34;

因此,您需要$("input").attr("valid-file")中的2个参数来设置任何属性

试试这个:http://plnkr.co/edit/lSl3GdZrRAyFps5QOSlw?p=preview

.attr()

HTML:

 $("select").change(function(){
    if ($("select option:selected").val() == "0") {
      $("input").attr("valid-file",""); // try this
    } else {
      $("input").removeAttr("valid-file");
    }
  });
相关问题