$(this).val() 在使用 .on(‘input’) 时返回为空。

huangapple go评论42阅读模式
英文:

$(this).val() returning empty on WTF with .on('input')

问题

我在Flask应用程序中有一个WTForm文本字段,其ID为start_date

<div class="col-2 col-2-xsmall" id="start_date">
    {{ form.start_date(placeholder="Start Date") }}
</div>

我还有随后的jQuery代码,我正在尝试将其记录到控制台中。

var $start_date = $('#start_date');
$start_date.on('input', function(){
    console.log($(this).val());
    console.log("Hello");
});

最终目标是在字段中匹配正则表达式时更新DOM,但在开发过程中,当我记录$(this).val()时,控制台返回一个空字符串。额外的日志仅用于检查,当我在字段中按任意键时,它确实显示在控制台中。

这是当我输入数字1991时的控制台,我期望上面每个"Hello"之前的输出应该是字段中的值,而不是我收到的空字符串。

英文:

I have a WTForm text field in a flask application with the id start_date

&lt;div class=&quot;col-2 col-2-xsmall&quot; id=&quot;start_date&quot;&gt;
    {{ form.start_date(placeholder=&quot;Start Date&quot;) }}
&lt;/div&gt;

I also have subsequent jQuery that I'm trying to log into the console.

var $start_date = $(&#39;#start_date&#39;)
	$start_date.on(&#39;input&#39;, function(){
		console.log($(this).text());
		console.log(&quot;Hello&quot;)

The end goal is to update the DOM when a regular expression is matched in the field, but during development, when I log $(this).val(), I'm getting back an empty string in the console. The additional log is just to sanity check, which does show up in the console when I press any key in the field.

nilread.wkx http://localhost:5000/query.html?query_id=1
main.js:217 
main.js:218 Hello

main.js:217 
main.js:218 Hello

main.js:217 
main.js:218 Hello

main.js:217 
main.js:218 Hello

This is the console when I type the number 1991, where I would expect the output above each Hello to be the value that is in the field instead of the empty string I'm getting.

答案1

得分: 2

我不确定是否理解正确,但首先,您为什么在类型为日期的输入字段上使用div?此外,您提到要获取输入字段的值,但却使用了文本方法,为什么?

您应该使用HTML input 标签代替div,如果只想使用日期类型,请添加type="date",如下所示:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var start_date = document.getElementById('start_date');
start_date.addEventListener('input', function(event) {
    console.log(event.target.value);
    console.log("Hello");
});

<!-- language: lang-html -->

<input class="col-2 col-2-xsmall" id="start_date" value='{{ form.start_date(placeholder="Start Date") }}'>

<!-- end snippet -->

注意:我使用了纯JS,因为在代码片段中不识别jQuery,这样您可以执行它并查看是否符合您的要求。已将jQuery的注释代码提供。

英文:

I am not sure to have well understood, but first, why are you using a div for an input of type date ?
Also, you have talked about getting value of the input, but you use text method : why ?

Instead of a div, you should use HTML input (add type=&quot;date&quot; if you want only to have date type) tag like this :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

/*
var $start_date = $(&#39;#start_date&#39;);
$start_date.on(&#39;input&#39;, function(event) {
    console.log(event.val());
    console.log(&quot;Hello&quot;);
});
*/

var start_date = document.getElementById(&#39;start_date&#39;);
start_date.addEventListener(&#39;input&#39;, function(event) {
    console.log(event.target.value);
    console.log(&quot;Hello&quot;);
});

<!-- language: lang-html -->

&lt;input class=&quot;col-2 col-2-xsmall&quot; id=&quot;start_date&quot; value=&#39;{{ form.start_date(placeholder=&quot;Start Date&quot;) }}&#39;&gt;

<!-- end snippet -->

Note : I have used Vanilla JS because JQuery is not recognized in the snippet, so that you can execute it and see if it is what you want. The commented JQuery code should work.

huangapple
  • 本文由 发表于 2023年6月19日 01:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定