【发布时间】:2016-01-23 14:02:14
【问题描述】:
使用 jQuery,我将文本框附加如下:-
$.each(myDataJSON, function (index, value)
{
var newRow = "<tr><td><input type='text' class='txtBox' value="+value.Price+"/></td></tr>";
$('#myTable').append(newRow);
})
现在,在添加行之后,会呈现所需的 html。现在我将模糊事件与这些文本框相关联,如下所示:-
$(document).on('blur','.txtBox',function(e)
{
var newTextValue = $(this).val();
console.log(newTextValue);//this throws below error
//Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
});
但是,如果我尝试修补它。然后上述错误消失,但它不反映新的更新值而是旧值。仅对第一个动态添加的文本框,它给出新的更新值,而不是其他文本框。
$(document).on('blur','.txtBox',function(e)
{
var newTextValue = $('.txtBox').val();
console.log(newTextValue);//no error
});
奇怪的是,当我在检查窗口中检查 html 时,文本框的值都没有更新。
我的问题是
如何为动态添加的文本框获取文本框的更新值
另外, 除了 focusout/blur 是否有任何事件,以便我可以在用户离开文本框的那一刻获得更新的文本框值,而不管等待文本框外的点击事件
【问题讨论】:
-
console.lo(应该是console.log(拼写错误。你在哪里用过toLowerCase()?? -
这里输入问题时输入错误
-
尝试
this.val()而不是$(this).val() -
checked Uncaught TypeError: this.val is not a function
-
做
$(e.target).val()
标签: javascript jquery html