以下代码可以很好地找到保存在标签字段中的价格值。但是我需要在文本区域中保存数量,但它不起作用。
有什么想法吗?
Javascript:
var price = parseInt(ui.draggable.find(".price").html().replace("$ ", ""));
var qty = parseInt(ui.draggable.find(".qty").html());
HTML:
<label class="price">$ 100</label>
<input name="qty_1" type="text" class="qty" id="qty_1" value="1" size="2" maxlength="2">
最佳答案
要设置/获取表单元素的值,应使用 val
方法而不是 html/text
。
var qty = parseInt(ui.draggable.find(".qty").val(), 10);
此外,您还应该在 parseInt
中指定 radix
。
parseInt(string[, radix])
radix
: An integer that represents the radix of the above mentioned string. While this parameter is optional, always specify it to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.
关于javascript - jquery 查找文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12538371/