简体   繁体   English

为什么我无法获得此var的值?

[英]Why can't I get the value of this var?

I want to create a live change field input system with jQuery. 我想使用jQuery创建实时更改字段输入系统。 This is my code: 这是我的代码:

 $(function() { $('td').on('dblclick', function() { var tdValue = $(this).text(); var tdTag = $(this).html(); tdTag = '<input id="newClass" type="text" value="' + tdValue + '">'; $(this).html(tdTag); $('input#newClass').on('change', function() { var inputVal = $(this).val(); alert('inputVal = ' + inputVal); var inputTag = $(this).html(); alert('inputTag = ' + inputTag); var var3 = '<span> var3 ' + inputTag + '</span>'; alert(var3); }) }) }) 

Why is inputTag and var3 empty? 为什么inputTagvar3空? This is my (very simple) html code 这是我的(非常简单的)HTML代码

我的小HTML代码

Note that the td tags are repeated using a PHP while loop. 注意,使用PHP while循环重复td标签。 Thank you in advance. 先感谢您。

Now I don't have enough reputation to comment and I'm still confused. 现在我没有足够的声誉来发表评论,我仍然感到困惑。 Try following codes and see if anything suits you. 尝试遵循以下代码,看看是否有适合您的代码。

var inputTag = $(this)[0].outerHTML;

or 要么

var inputTag = $(this).parent()[0].outerHTML;

or 要么

var var3 = '<span>' + inputVal + '</span>';
$(this).after(var3);
$(this).remove();

or if you just want to make the input field read only, try with 或者,如果您只想使输入字段为只读,请尝试使用

$(this).prop("readonly", true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM