twitter-bootstrap 使用Bootstrap使用onclick按钮在模态窗口中设置文本框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32004291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 23:03:41  来源:igfitidea点击:

Setting textbox value in modal window using onclick button using Bootstrap

javascriptjqueryhtmltwitter-bootstrap

提问by Varun

I ma trying to display username in modal text box using onclick button I have a button from there on onclicki am calling onclick="getUserName(this.id)"

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

this.id 

gives me name which i want to set in

给我我想设置的名字

modal textbox userName

Here is my function

这是我的功能

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

I am getting username in alert(username)but how do i set thsi value to my modal, username text field

我正在输入用户名,alert(username)但如何将 thsi 值设置为我的模式、用户名文本字段

Here is my Modal

这是我的模态

<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> 

回答by DavidDomain

This should work.

这应该有效。

But you should also take a look at the Bootstrap documentation about modal windows. There is a section explaining how to vary modal content.

但是您还应该查看有关模态窗口的 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.

使用 event.relatedTarget 和 HTML data-* 属性(可能通过 jQuery)根据单击的按钮改变模式的内容。

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>

回答by Norlihazmey Ghazali

I think the problem is you are using showevent,instead try call shownevent after all, like so :

我认为问题在于您正在使用show事件,而shown毕竟尝试调用事件,如下所示:

function getUserName(username) {
  $('#editUserPopUp').modal('show');

  $('#editUserPopUp').on('shown', function () {
    alert(username);
    $("#userName").val(username);
  })

}

p/s : did't test it. Hope this help. Comment if did't work.

p/s : 没有测试过。希望这有帮助。如果不起作用,请发表评论。