Bootstrap - 编辑Modal背后的TextArea

时间:2017-01-24 14:39:35

标签: javascript jquery twitter-bootstrap

我有一个使用Bootstrap 3的网页。在这个页面中,我需要一个人可以回答的文本区域。答案是故事问题。故事问题出现在模态对话框中。可以在此Bootply中查看设置。代码如下所示:

<div class="container">
  <form>
    <div class="form-group">
      <label>Answer (<a id="showProblemLink" href="#">view problem</a>)</label>
      <textarea class="form-control" rows="7">      </textarea>
    </div>
  </form>

  <div id="problemModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-body">
            <p>
            The local school wants a new playground.
            The lot they can use for the playground is shaped like a rectangle.
            The school has decided to let the students design the new playground.
            The students have decided that they want 1/4 of the playground to be a football field.
              Another 3/8 of the lot will be used for a basketball court.
              How much of the lot is remaining for the slides and swings?
            </p>
        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>        
      </div>      
    </div>
  </div>  
</div>

我的用户希望能够在输入答案时在模态窗口中打开故事问题。这将让他们仔细检查他们的答案。显而易见的答案是让他们重新打开对话框。但是,显然,这还不够好。我真的需要保持对话框打开,让用户在其后面/下面的文本区域中输入数据。对我来说似乎很奇怪。但是,这就是我要做的事情。

我的问题是,当我点击textarea时,对话框消失了。我尝试添加data-backdrop="false"。虽然保持对话框打开,但它并没有让我实际编辑textarea。我错过了什么?

9 个答案:

答案 0 :(得分:4)

实际上,让完全你想要的东西非常简单。

enter image description here

您实际需要的是对话,而不是模态。

这是使用jQuery UI对话的working example

<强>解释

  • 模式旨在阻止用户与应用程序的交互。
  • dialog外观和行为类似,但旨在让用户继续与页面进行交互(如果需要,可以手动设置以阻止所有内容)

这是关于它的SO discussion

有关示例的一些详细信息:

默认情况下,标题可以拖动对话框。我保持示例类似于你的模态,所以我使用:

取出标题
dialogClass: 'dialog-modal'

.dialog-modal .ui-dialog-titlebar {
  display:none;
}

为了使整个对话框可以拖动我使用:

draggable: false

.parent().draggable();

如果您确实需要标题,则可以删除所有这些部分。这是example with a header

答案 1 :(得分:2)

  

我的问题是,当我点击textarea时,对话框消失了。一世   尝试添加data-backdrop =“false”。

这是因为你实际上没有点击textarea,你点击了一个覆盖元素,它会在点击时关闭模态。

我不确定删除叠加元素是否能够在文本区域输入,但即便如此,我认为这不是一个好主意(删除叠加层并且能够在模态为时执行某些操作还在吗。

所以我的建议是: 点击时在模态上有一个按钮,它应该显示另一个textarea。当您在textarea上输入内容时,它会更新主文本区域内的内容。

所以这是新模式:

enter image description here enter image description here

见工作example

答案 2 :(得分:2)

您想要实现的目标并非易事。

  • 隐藏模态背景 - 轻松
  • 移动整个模态div - medium
  • 使模式后面的文本区域成为焦点 - 非常难以

我知道您希望节省页面空间,这就是为什么我会建议最干净的选项,并且最接近您要实现的目标:Bootstrap Collapse

DEMO here

enter image description here

答案 3 :(得分:1)

我能想到的一些解决方案是:

  1. 将您的回答textarea移至包含问题的对话框。

  2. 或使用popover而不是模态。

  3. 所以here is my example使用弹出而不是模态。

    Stackoverflow片段,看起来很糟糕,因为它缺少所有引导样式,所以检查bootply链接:

    $(".pop-div > a").popover({
      html: true,
      placement: "left",
      trigger: "click",
      title: function() {
        return $(this).siblings('.pop-title').text()
      },
      content: function() {
        return $(this).siblings('.pop-content').text()
      }
    });
    .pop-div {
      display: inline;
    }
    .pop-title,
    .pop-content {
      display: none;
    }
    .popover-title {
      font-size: 15px;
    }
    .popover-content {
      font-size: 10px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <div class="container">
      <form>
        <div class="form-group">
          <label>Answer (
            <div class="pop-div">
              <a href="javascript:void(0);">view problem</a>
              <div class="pop-title">Question</div>
              <div class="pop-content">
                <p>
                  The local school wants a new playground. The lot they can use for the playground is shaped like a rectangle. The school has decided to let the students design the new playground. The students have decided that they want 1/4 of the playground to be a football
                  field. Another 3/8 of the lot will be used for a basketball court. How much of the lot is remaining for the slides and swings?
                </p>
              </div>
            </div>
            )</label>
          <textarea class="form-control" rows="7"></textarea>
        </div>
      </form>
    </div>

答案 4 :(得分:0)

由于这是一个奇怪的问题,这需要一些小小的问题,但这是我提出的解决方案。

我在内部创建了隐藏在视图中的模态,并将捕获模态打开时完成的任何输入。当用户关闭模态时,焦点将被发送回真正的文本区域。

HTML(略有修改):

<div class="container">
  <form>
    <div class="form-group">
      <label>Answer (<a id="showProblemLink" href="#">view problem</a>)    </label>
      <textarea id="outside-modal" class="form-control" rows="7">          </textarea>
    </div>
  </form>

  <div id="problemModal" class="modal" tabindex="-1" role="dialog" data-backdrop="false">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-body">
            <p>
              The local school wants a new playground.
              The lot they can use for the playground is shaped like a rectangle.
              The school has decided to let the students design the new playground.
              The students have decided that they want 1/4 of the playground to be a football field.
              Another 3/8 of the lot will be used for a basketball court.
              How much of the lot is remaining for the slides and swings?
            </p>
            <textarea id="inside-modal"></textarea>
        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>        
      </div>      
    </div>
  </div>  
</div>`

CSS:

textarea#inside-modal {
  position:absolute;
  left:-4000px;
}

jQuery的:

$("#showProblemLink").on("click", function() {
    //show the modal
  $('#problemModal').modal('show');
  //fill the hidden textarea with the real textarea's contents
  $('#problemModal textarea#inside-modal').val($('textarea#outside-modal').val());
  //capture user input in modal and send to outside textarea
  $('#problemModal textarea#inside-modal').on('input',function(e) {
    //fill the outside textarea with the inside textarea's contents
    $('textarea#outside-modal').val($('textarea#inside-modal').val());
  }).focus();
});
//when modal closed, send focus back to textarea
$("#problemModal").on('hidden.bs.modal',function() {
  $('textarea#outside-modal').focus();
});

请参阅this bootply

答案 5 :(得分:0)

另一种解决方案是使用Popover,在textarea的焦点事件中,你有一个“类似模态”的功能。

这样做的好处是,您可以保留用户专注于哪个textarea属于哪个故事的能力,就像当您删除叠加层时,您可能会丢失需要模态的位置的上下文,或者用户执行的操作如打开多种模态等。

http://getbootstrap.com/javascript/#popovers

答案 6 :(得分:0)

我制作了一个代码段here,可以满足您的需要,我会在显示模式时使用textareaevent.key手动输入event.keyCode

&#13;
&#13;
var $text = $('#textarea');
var backEdit = false;
var $modal = $('#problemModal');
$("#showProblemLink").on("click", function() {
  $modal.modal('show');
});

$modal.on('shown.bs.modal', function() {
  backEdit = true;
})
$modal.on('hidden.bs.modal', function() {
  backEdit = false;
})
$(document).keypress(function(e) {
  if (backEdit) {
    var keyCode = e.keyCode;
    formatTextArea(keyCode);
  }
});

function formatTextArea(keycode) {
  var val = $text.val();
  var caretPos = $text[0].selectionStart;
  var textAreaTxt = $text.val();
  $text.val(textAreaTxt.substring(0, caretPos) + String.fromCharCode(keycode) + textAreaTxt.substring(caretPos));
}
&#13;
<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="container">
  <form>
    <div class="form-group">
      <label>Answer (<a id="showProblemLink" href="#">view problem</a>)</label>
      <textarea class="form-control" rows="7" id="textarea"></textarea>
    </div>
  </form>

  <div id="problemModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-body">
          <p>
            The local school wants a new playground. The lot they can use for the playground is shaped like a rectangle. The school has decided to let the students design the new playground. The students have decided that they want 1/4 of the playground to be a football
            field. Another 3/8 of the lot will be used for a basketball court. How much of the lot is remaining for the slides and swings?
          </p>
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 7 :(得分:0)

模态是每个定义'模态'又名'总是在顶部对话框',所以你所要做的就是使它非模态,使用你的html布局这可以通过jQuery完成:

$('#problemModal').removeClass('modal')
    .hide()
    .css('position','absolute')
    .css('top',0)
    .css('left',(screen.width - $('#problemModal').width())/2);
$("#showProblemLink").on("click", function() {
    $('#problemModal').show();
});

$('#modal-close-btn').click(function(){
    $('#problemModal').hide();
});

这是一个有效的Bootply作为演示。

答案 8 :(得分:0)

您可以停用模式&#34;强制对焦&#34; (例如:通过删除文档事件&#34; focusin.modal&#34;来阻止外部文本区域的编辑:

$("#problemModal").on("shown.bs.modal", function() {
    $(document).off("focusin.modal");
});

如果你想在模态打开后将光标聚焦在textarea上,只需添加$("textarea").focus();

$("#problemModal").on("shown.bs.modal", function() {
    $(document).off("focusin.modal");
    $("textarea").focus();
});
相关问题