开发者

Syntax error - how to get value of textbox

开发者 https://www.devze.com 2023-03-14 21:36 出处:网络
I have a trouble getting value from text box. Conventional javascript getElementByName returns error :undefined. I was able to use jQuery 开发者_运维问答in other examples with select. But can\'t find

I have a trouble getting value from text box. Conventional javascript getElementByName returns error :undefined. I was able to use jQuery 开发者_运维问答in other examples with select. But can't find a reference anywhere about input tags. This line shows syntax error:

  var total = = $('input[name='+formQty+'] :text').val();

What is a jquery for picking value/text of a textbox?


you have 2 equal signs.

var total = $('input[name='+formQty+'] :text').val();

It might be easier also to stick an ID attribute on the input tag and use this

var total = $('#myInputId').val();

make sure you are calling it from a loaded dom:

$(function() {
   var total = $('#myInputId').val();
});


From what I understand your query selector is incorrect.

It should be 'input[name='+formQty+']:text' the :text as you have it is trying to select any text elements underneath the current input resulting in nothing. You can even get rid of the :text and only select the element based on name. .val() should then work if you have properly selected the input element.

You should also debug this by just performing the $('...') method without the function call and see what elements are returned.


Regarding your problem getting this to work with getElementByName() - that's probably because the method is actually getElementsByName() - note the plural elementS. It returns a list of all elements with the specified name, accessible via array syntax; even if there is only one matching element you still get a list (with only one thing in it).

If you are sure there will be exactly one element with the specified name you can do this:

var elementValue = document.getElementsByName(name)[0].value;
0

精彩评论

暂无评论...
验证码 换一张
取 消