javascript - 使用 Bootstrap 使用 onclick 按钮在模式窗口中设置文本框值

标签 javascript jquery html twitter-bootstrap

我试图使用 onclick 按钮在模式文本框中显示用户名 我在 onclick 上有一个按钮,我正在调用 onclick="getUserName(this.id)"

this.id 

给我我想要设置的名称

modal textbox userName

这是我的函数

function getUserName(username) {
      $('#editUserPopUp').bind('show',function(){
          alert(username);
          $("#userName").val(username);
      });
 }

我在 alert(username) 中获取用户名 但我如何将这个值设置为我的模态用户名文本字段

这是我的模态框

<div class="modal fade" id=editUserPopUp tabindex="-1" 
    role="dialog" aria-labelledby="helpModalLabel" aria-hidden="true">
  <div class="modal-dialog ">
    <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="myModalLabel">Update Password</h4>
        </div>
        <div class="modal-body">                                                                                    
        <form class="EditUserBody" role="form" id="usermanagement_editUser="novalidate" method="POST">
          <div class="input-group col-md-8">
           <span class="input-group-addon">User Name</span>
             <input class="form-control" id="userName" type="text" class="input-medium" disabled />
          </div>           
        </form> 
       </div>   
    </div>
  </div>
</div> 

最佳答案

这应该有效。

但是您还应该查看有关模式窗口的 Bootstrap 文档。有一节解释了如何改变模态内容。

Varying modal content based on trigger button

Use event.relatedTarget and HTML data-* attributes (possibly via jQuery) to vary the contents of the modal depending on which button was clicked.

function getUserName(username) {
  var $modal = $('#editUserPopUp'),
      $userName = $modal.find('#userName');
  $userName.val(username);
  $modal.modal("show");
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>


<button onclick="getUserName(this.id)" id="Foo Bar">Open Modal</button>

<div class="modal fade" id=editUserPopUp tabindex="-1" 
    role="dialog" aria-labelledby="helpModalLabel" aria-hidden="true">
  <div class="modal-dialog ">
    <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="myModalLabel">Update Password</h4>
        </div>
        <div class="modal-body">                                                                                    
        <form class="EditUserBody" role="form" id="usermanagement_editUser="novalidate" method="POST">
          <div class="input-group col-md-8">
           <span class="input-group-addon">User Name</span>
             <input class="form-control" id="userName" type="text" class="input-medium" disabled />
          </div>           
        </form> 
       </div>   
    </div>
  </div>
</div>

关于javascript - 使用 Bootstrap 使用 onclick 按钮在模式窗口中设置文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32004291/

相关文章:

javascript - 一键上传文件并发布文本输入值?

javascript - 如何覆盖 div 中除一个元素之外的所有内容

php - jQuery 自定义添加属性

html - 如何在我的代码中自由移动另一个 div 中的 div 元素?

javascript - $(window).height() 值在 Firefox 22.0 中是常量

javascript - 使用鼠标指针简单地交换水平和垂直滚动

javascript - 如何根据多个字符串进行排序

javascript - 获取响应式嵌入图像的高度

javascript - jQuery(...).yiiGridView 不是函数

javascript - 保留 HTML 到文本区域的格式