简体   繁体   中英

How to get a value from textarea within modal popup?

There is this http://jsfiddle.net/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

尝试这个,

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

replace

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

with

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

here is an example how to take out the value. https://jsfiddle.net/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())
})

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