简体   繁体   中英

How do I reference an html form field inside Twitter Bootstrap's pop-up modal?

My page has a form fields like so.

<form id = "registerform" class="form-horizontal" action = "insert.php" method = "post">
  <div class="control-group">
    <label class="control-label" for="inputTeamName">Team Name</label>
      <div class="controls">
        <input id="inputTeamName" class = "span11" type="text" placeholder="Team Name" name = "inputTeamName" data-toggle="tooltip" title="Innuendos encouraged" data-placement="left">
      </div>
    </div>
  <div class="control-group">
    <div class="controls">
      <a href="#myModal" role="button" class="btn" id="registerBtn" data-toggle="modal">Register</a>
    </div>
  </div>
</form>

When the user clicks the register button, a modal loads

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Confirm Registration</h3>
  </div>
  <div class="modal-body">
    <ul class="unstyled">
      <li>Team Name:</li>
    </ul>
  </div>
<div class="modal-footer">
  <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
  <button class="btn btn-primary">Confirm</button>
</div>

It looks like this - http://twitter.github.io/bootstrap/javascript.html#modals

I want to use the modal to confirm the user's input. How do I reference the text the user entered in the form field and display it inside of the modal?

Thanks

To be clear, I added a div tag inside <li>Team Name:</li> :

<div class="modal-body">
    <ul class="unstyled">
      <li>Team Name:<div id = "divTeamName"></div></li>
    </ul>
</div>

Add the following script (possibly just before the closing html tag):

<script type="text/javascript">   
   $("#registerBtn").click(function() {          
    $(divTeamName).text($('#inputTeamName').val());        
   });
</script>

The JQuery code above will respond after your register button has been clicked, displaying the text from the input into the div I've added above.

JsFiddle:

http://jsfiddle.net/6y4yQ/1/

   <script type="text/javascript">   
   $("#inputTeamName").keypress(function() {    
   var TeamName = $(this).val();        
   $(this).after(TeamName);        
   });             
   </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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