[英]Getting strange error while fetching dynamic added textbox value?
使用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時,沒有任何文本框值被更新。
我的問題是
另外, 是否有除焦點/模糊之外的任何事件,以便我可以獲取更新的文本框值,無論用戶何時等待離開文本框之外的單擊事件,我都可以離開文本框
“ 試圖手動執行功能時,”因為你有一個不同的失敗this
。 事件指向調用者
你必須做
$(document).on('blur','.txtBox',function(e)
{
var newTextValue = $(e.target).val();
});
這段代碼對我有用(記錄正確的值):
<html>
<body>
<table id = "myTable">
</table>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
var newRow = "<tr><td><input type='text' class='txtBox' value='1.1'/> </td></tr>";
$('#myTable').append(newRow);
var newRow1 = "<tr><td><input type='text' class='txtBox' value='1.2'/></td></tr>";
$('#myTable').append(newRow1);
$(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
});
</script>
</html>
也許您應該檢查是否還有其他問題?
<html>
<head>
<link href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.min.css" rel="stylesheet"/>
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>
<script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<script>
$(document).ready(function () {
$(document).on('blur', '.txtBox', function (e) {
var newTextValue = $(this).val();
alert(newTextValue)
});
$('#addRow').click(function () {
var szTr = "<tr><td><input type='text' class='txtBox' value='10'/> </td></tr>";
$('#myTable').append(szTr);
});
});
</script>
</head>
<body>
<button id="addRow" value="Add Row">Add Row</button>
<table id="myTable">
<thead>
<tr>
<th>Headline</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.