X-editable:如何给输入框添加一个额外的属性?

X-editable: How to add an extra attribute to the input box?

我有一个 link 启用了 x-editable data-type="text"

我想向编辑项目时创建的 input 字段添加一个附加属性。

所以目前 x-editable 正在生成以下输入字段:

<input type="text" class="form-control input-sm" style="padding-right: 24px;">

我想添加一个模式属性,所以它看起来像(添加模式属性):

<input type="text" class="form-control input-sm" style="padding-right: 24px;" pattern="blah">

如何添加这个额外的属性?

您可以使用 attr 方法设置任何值(我们没有 addAttr 方法,而 class addClass 我们只需要使用 element.attr('AttributeName','AttributeValue')).

如果您希望删除该属性,则可以使用以下语法。

  element.removeAttr('AttributeName')

$('input').attr('pattern','desiredValue');
alert($('input').attr('pattern'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="form-control input-sm" style="padding-right: 24px;">

请注意,每次单击 x-editable 元素都可以访问生成的输入 在弹出窗口中使用

$('your element').on("click",function(){
  $(this).next().find(".editable-input input").attr("data-pattern","yep 1!")
});

查看下面的代码片段:使用 inspect 你会看到 pattern 属性存在

$(function() {
    $('#username1').editable();
    
    $('#username1').on("click",function(){
      $(this).next().find(".editable-input input").attr("data-pattern","yep 1!")
    });
    
    $('#username2').editable();
    
    $('#username2').on("click",function(){
      $(this).next().find(".editable-input input").attr("data-pattern","yep 2!")
    });
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/css/bootstrap-editable.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/js/bootstrap-editable.min.js"></script>
<br><br><br><br><br>
<a href="#" id="username1" data-type="text" data-pk="1" data-title="Enter username">superuser 1</a>
<br><br><br><br>
<a href="#" id="username2" data-type="text" data-pk="2" >superuser 2</a>

您可以使用 jQuery 中的 .attr() 方法:

$('.input-sm').attr("pattern","blah")