[英]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. 我复制/粘贴了这个http://jsfiddle.net/WV5e7/模态,目前正在使用。
here is the code: js 这是代码:js
$('#myModal').on('shown.bs.modal', function () {
$('#textareaID').focus();
})
html: 的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">×</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 因此,我目前不知道如何获取使用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
https://jsfiddle.net/WV5e7/315/看看是否适合您
$('#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.
因为您需要处理click事件。
And use this code: 并使用以下代码:
<button type="button" class="btn btn-primary save">Save changes</button>
$(".save").on("click", function() {
console.log($("#textareaID").val())
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.