单击模式引导程序内的按钮后的Textarea

时间:2017-07-19 04:07:48

标签: javascript html bootstrap-modal

我有一个简单的模态,我想在模态中添加textarea,但我希望在单击模态内文本下的按钮后可以看到textarea

在这里摆弄:http://jsfiddle.net/nyderetna/WV5e7/344/

HTML:

<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg launch-modal" data-toggle="modal" data-target="#myModal">
  Launch modal
</button>

<!-- Modal -->
<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-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
      <p>Some text here</p>
      <div>another foo text</div>
      <textarea hidden id="textarea-message"></textarea>
        <button class="btn btn-reply btn-sm btn-success pull-right">Reply that foo!</button>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->

JS:

$('#myModal').on('shown.bs.modal', function () {
    $(document).on('click', '.btn-reply', function () { 
    var text = $('#textarea-message').val();
    var html = '<input type="textarea" name="message" placeholder="Enter Sandman message ..." class="form-control" id="textarea-message" autocomplete="off" maxlength="200">';

    $(document).find('#textarea-message').append(html);
        });
    $('#textarea-message').show();
})

$(document).find('#textarea-message').on('keypress', function(e){

    if ( e.which == 13 ) {
        e.preventDefault();
        $('.btn-reply').trigger('click');
    }

});

CSS:

.launch-modal {
    margin: 20px;
}

有人可以帮我解决这个问题吗?

EDITED

如前所述,我忘了告诉我要按Enter后输入文字显示。我还将textarea更改为text

更新了代码 here

1 个答案:

答案 0 :(得分:1)

我更新了你的小提琴check here

只需为按钮提供id并使用按钮点击事件就可以了

$('#btnReply').click(function () {    
    $('#textarea-message').removeClass('hide');
});

$('#myModal').on('shown.bs.modal', function () {

	$(document).on('click', '.btn-reply', function () { 
    var text = $('#textarea-message').val();
    var html = '<input type="textarea" name="message" placeholder="Enter Sandman message ..." class="form-control" id="textarea-message" autocomplete="off" maxlength="200">';
   
});

});
$('#btnShowModal').click(function () {
	$('#textarea-message').addClass('hide');
});
$('#btnReply').click(function () {

	$('#textarea-message').removeClass('hide');
});
.launch-modal {
    margin: 20px;
}
<html lang="en">
<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<!-- Button trigger modal -->
<button  id="btnShowModal" class="btn btn-primary btn-lg launch-modal" data-toggle="modal" data-target="#myModal">
  Launch modal
</button>

<!-- Modal -->
<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-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
      <p>Some text here</p>
      <div>another foo text</div>
      <textarea class="hide" id="textarea-message"></textarea>
        <button id="btnReply" class="btn btn-reply btn-sm btn-success pull-right">Reply that foo!</button>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

</body>
</html>