i wanted to get the value of my textbox in javascript. this is my javascript-
<script type="text/javascript">
var submitted = false; var type;
window.onbeforeunload = function () {
if (!submitted) {
return "Are you sure you want to leave this page, your test will not be saved?"+type;
}
};
document.getElementById('form1').onsubmit = function () {
type=document.getElementById('TextBox1').value;
submitted = true;
};
</script>
no matter what i type it does not give me the value of textbox1
<script type="text/javascript">
var submitted = false; var type;
$(document).ready(function ()
{
$('#form1').submit(function ()
{
type = document.getElementById('TextBox1').value;
submitted = true;
});
window.onbeforeunload = function ()
{
if (!submitted)
{
return "Are you sure you want to leave this page, your test will not be saved?" + type;
}
};
});
</script>
if you want to display type's value in alert box then,change if(!submitted') to if(submitted) and you will receive an alert with type value.hope this will do.
This looks like just a logic problem:
When you run onsubmit
, it sets submitted
to true, and then it runs onbeforeunload
. Right now it it basically only returns the input value if you've not run onsubmit
. If you take the !
out of (!submitted)
, it will give you an alert with the input's value.
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.