gpt4 book ai didi

php - jQuery 使用 id 选择器返回错误值

转载 作者:行者123 更新时间:2023-12-01 05:42:28 25 4
gpt4 key购买 nike

我想我正在慢慢陷入疯狂。在我的代码中我有

<script>
console.debug("1) <?=$_SESSION["searchEndDate"]?>");
console.debug("2) " + $("#searchEndDate").val()); //if another element would exist with that it, it would give me that elements value here
</script>

<input type="text" class="dateInput" id="searchEndDate" value="<?=$_SESSION["searchEndDate"]?>" />

<script>
console.debug("3) " + $("#searchEndDate").val());
console.debug("4) <?=$_SESSION["searchEndDate"]?>"); // just to be sure it wasn't interfered anywhere
</script>

控制台输出奇怪地说

1) 03.05.2015 //correct
2) undefined //correct (no other elements with the same id)
3) 01.05.2015 //wait...what?
4) 03.05.2015 //still correct, session was not interfered

...如果我查看页面的 html 源代码,value 元素显然包含正确的值。调试仍然包含错误的值...当我稍后在代码中初始化日期选择器时,日期选择器将具有错误的日期。我真的很困惑...

我想念什么?

<小时/>

[编辑]:是的,我知道日期选择器的实际初始化必须在文档就绪函数中进行。但这不是重点!请在回答前仔细阅读问题!

[编辑2]:使用了jQuery:

jQuery core 1.10.2
jQuery ui 1.10.4
jQuery mobile 1.4.5

最佳答案

好的,我在本地主机上启动并运行了以下代码:

<?php
session_start();

$_SESSION['searchEndDate'] = '03.05.2015';
?>

<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>

<script>
console.debug("1) <?=$_SESSION["searchEndDate"]?>");
console.debug("2) " + $("#searchEndDate").val()); //if another element would exist with that it, it would give me that elements value here
</script>

<input type="text" class="dateInput" id="searchEndDate" value="<?=$_SESSION["searchEndDate"]?>" />

<script>
console.debug("3) " + $("#searchEndDate").val());
console.debug("4) <?=$_SESSION["searchEndDate"]?>"); // just to be sure it wasn't interfered anywhere
</script>

我的控制台输出是这样的:

1) 03.05.2015
2) undefined
3) 03.05.2015
4) 03.05.2015

也许您的浏览器历史记录有问题,正在尝试自动完成它知道的输入字段?尝试类似 - http://www.w3schools.com/tags/att_input_autocomplete.asp

关于php - jQuery 使用 id 选择器返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29918774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com