admin管理员组文章数量:1429926
Any idea what is best practice for getting the value out of a form input field (that is second in the markup after it's label)?
I have been able to get the rest of the values fine (SKUs and price) but quantity is tricky.
JSFIDDLE to whole thing: /
Here is some jQuery that scopes the first tr of the table and then attempts to get into that row and pull out the quantity value:
var BSKfirstQTY = $(BSKfirstrow).find('td.quantity').text('input value');
But as you can see from the fiddle's result pane, it's spewing out [object Object]
.
NOTE: Leaving the .text() empty results in the it picking up the word Qty in the fiddle result pane (which is the label, not the number).
Any help would be hugely appreciated :)
Any idea what is best practice for getting the value out of a form input field (that is second in the markup after it's label)?
I have been able to get the rest of the values fine (SKUs and price) but quantity is tricky.
JSFIDDLE to whole thing: http://jsfiddle/hslincoln/7HXBQ/33/
Here is some jQuery that scopes the first tr of the table and then attempts to get into that row and pull out the quantity value:
var BSKfirstQTY = $(BSKfirstrow).find('td.quantity').text('input value');
But as you can see from the fiddle's result pane, it's spewing out [object Object]
.
NOTE: Leaving the .text() empty results in the it picking up the word Qty in the fiddle result pane (which is the label, not the number).
Any help would be hugely appreciated :)
Share Improve this question asked Mar 19, 2014 at 14:16 Harry LincolnHarry Lincoln 6562 gold badges10 silver badges30 bronze badges 2-
I sometimes prefer the jQuery/native hybrid:
$(BSKfirstrow).find('td.quantity')[0].value
– Martijn Commented Mar 19, 2014 at 14:21 - It's because you try to use .val() on your td instead of your input. instead of doing .find('quantity').val() just do .find('quantity input').val() – Robin Leboeuf Commented Mar 19, 2014 at 14:23
2 Answers
Reset to default 7If you pass an argument into text()
, you are setting the content of td.quantity
to that string, and JQuery will return the object for chaining. If you call text()
with no argument, you will get the text inside the element.
You should use .val()
to retrieve the values of input/textarea/selects elements, like so:
var BSKfirstQTY = $(BSKfirstrow).find('td.quantity input').val();
For more info on val vs. text, see this question.
Check out Difference between val() and text() He says that .text() won't work on input elements.
.val()
http://api.jquery./val/
.text()
http://api.jquery./text/
I think there's not really a "best practice" scenario, since they both do different things. It just seems like it's the same, but if you're working with a form you really want to use .val()
. Otherwise .text()
might suffice.
本文标签: javascriptval() or text() to find value of input in formStack Overflow
版权声明:本文标题:javascript - .val() or .text() to find value of input in form? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745525101a2661799.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论