Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap input dialog with radio buttons

Does anyone know of a Bootstrap modal dialog plugin that allows you to put radio buttons on it and collect the values that were selected?

like image 668
MB34 Avatar asked Jan 09 '15 22:01

MB34


1 Answers

You don't need anything special to do this. Your modal is part of your page, thus anything you can access on the page you can access inside the modal as well.

$('#myModal').modal('show');

$('input[type=radio]').click(function() {
  alert($(this).val());
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
          </div>
          <div class="modal-body">
            <label class="radio-inline">
              <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1"> 1
            </label>
            <label class="radio-inline">
              <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2"> 2
            </label>
            <label class="radio-inline">
              <input type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3"> 3
            </label>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

When the modal is popped, it has three radio buttons in it. When you select one, an alert fires that shows you the value of the selected radio button.

This demonstrates that you can add whatever controls you would to any page, and access the data within them to do whatever you need to do.

like image 133
MattD Avatar answered Sep 30 '22 23:09

MattD