最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get a value from textarea within modal popup? - Stack Overflow

programmeradmin6浏览0评论

There is this / modal which I copy/pasted and currently working with right now.

here is the code: js

$('#myModal').on('shown.bs.modal', function () {
    $('#textareaID').focus();
})

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">
        <textarea id="textareaID" class="form-control"></textarea>
      </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><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

It works as seen in the demo, but when someone hits 'save changes' I can't seem to get the value of the text area inside it.

I did

$('#textareaID').val();

But, but there is not value, since it is trying to get the value before the save changes button is clicked.

So, I currently do not know how to get the value of the text submited using jquery

There is this http://jsfiddle/WV5e7/ modal which I copy/pasted and currently working with right now.

here is the code: js

$('#myModal').on('shown.bs.modal', function () {
    $('#textareaID').focus();
})

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">
        <textarea id="textareaID" class="form-control"></textarea>
      </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><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

It works as seen in the demo, but when someone hits 'save changes' I can't seem to get the value of the text area inside it.

I did

$('#textareaID').val();

But, but there is not value, since it is trying to get the value before the save changes button is clicked.

So, I currently do not know how to get the value of the text submited using jquery

Share Improve this question edited Apr 7, 2017 at 9:02 Reporter 3,9365 gold badges35 silver badges49 bronze badges asked Apr 7, 2017 at 8:57 user7342807user7342807 3136 silver badges23 bronze badges 4
  • @Kinduser sorry, typo – user7342807 Commented Apr 7, 2017 at 8:57
  • @user7342807 please share your code with fixed code and reproduce the same problem – gurvinder372 Commented Apr 7, 2017 at 8:59
  • @gurvinder372 Its fixed! I just need to get the value of the textarea – user7342807 Commented Apr 7, 2017 at 9:01
  • In that case the answer provided by me isn't invalid, since that is the answer to your question only. – gurvinder372 Commented Apr 7, 2017 at 9:01
Add a ment  | 

5 Answers 5

Reset to default 3

Try this,

$("#myModal").find('#textareaID').val();

replace

$('#textareaID').value();

with

$('#textareaID').val();

here is an example how to take out the value. https://jsfiddle/WV5e7/315/ and see if that works for you

$('#myModal').on('shown.bs.modal', function () {
  $('#textareaID').focus();
  $('.btn.btn-primary').click( function () {
    alert($('#textareaID').val());
  });
})

Try this code

 <button type="button" class="btn btn-primary save-data">Save changes</button>

$('.save-data').on('click', function(){
    var getVal = $('#textareaID').val();
  if(getVal != '') {
    alert(getVal);
    $('#myModal').modal('hide');
  } else {
    alert('textarea is required.');
  }
})

Firstly, add an class to "Save Changes" button. Because you need handle click event.

And use this code:

<button type="button" class="btn btn-primary save">Save changes</button>

$(".save").on("click", function() {
    console.log($("#textareaID").val())
})
发布评论

评论列表(0)

  1. 暂无评论