简体   繁体   中英

Getting value of input box not working

I am having a problem with my html document. I am trying to find the value of the iput box below but it wont work. It has that the value is blank. I am running to document as a file in my computer, not on the web, if that makes a difference. This is my html code:

<p class='textcenter'>What would you like to scan</p><input id='scanbox1' type='text' name='scanboxtype'>

And this is my jquery:

var a;
a = $('input[name=scanboxtype]').val();
alert('a is '+a)

What ever i type in, the alert pops up as 'a is'. Thanks for the help.

You haven't triggered the alert either on submit or on change or any other events.

So you don't see the alert message with the value

Always result will be "a is"

Example:

Using .change()

$('input[name=scanboxtype]').change(function(){
    alert('a is '+ this.value); // or $(this).val()
});

Using .keyup()

$('input[name=scanboxtype1]').keyup(function(){
    alert('a is '+ this.value); // or $(this).val()
});

Refer LIVE DEMO

Maybe your code is running when the key is pressed for example, but the value is not yet in the input element? For example on 'keydown' or something?

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