简体   繁体   English

jQuery text()函数不接受input.val()

[英]jQuery text() function not accepting input.val()

I'd like to set up a preview area that shows what some inputted text looks like in situ. 我想设置一个预览区域,以显示一些输入文本的原位。 However, I'm struggling to get this jQuery function with text() to work. 但是,我正在努力使此jQuery函数与text()一起工作。

whilst this works (displays "some sample text" in #preview when the form is submitted: 尽管这可行(提交表单后,在#preview中显示“一些示例文本”:

$("form").submit(function () {
              $("#preview").text("some sample text").show();
              return false;
            });

this doesn't work, and shows nothing when I try to take the contents of the text field and use that instead in the preview box: 这不起作用,并且当我尝试获取文本字段的内容并在预览框中使用它时什么也没显示:

$("form").submit(function () {
              $("#preview").text($("input:first").val()).show();
              return false;
            });

Any idea why? 知道为什么吗? Probably dead simple and I'm just missing it. 可能简直太简单了,我只是想念它。

The code works fine in a fiddle. 该代码可以在小提琴中正常工作。 Make sure #preview has a declared height and width, otherwise it may not be visually distinct. 确保#preview具有声明的高度和宽度,否则可能在视觉上不明显。

See working demo: http://jsfiddle.net/js4XU/ 查看工作演示: http : //jsfiddle.net/js4XU/

最后,我将其重写为更多的“实时预览”,并使用keyup()函数捕获输入的val,而不是在表单提交后请求。

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

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