gpt4 book ai didi

javascript - 如何通过javascript将值传递到输入字段

转载 作者:行者123 更新时间:2023-11-28 13:07:51 24 4
gpt4 key购买 nike

我的问题:输入框中显示由 id 传递的值。但我在 html 源代码部分看不到该值。

这是我的输入字段,我在其中通过 cookie 传递一个值。

<input type="text" id="city" class="i-p-c" value="" autocomplete="off" placeholder="Select City">

我正在做的是:我有一个城市下拉菜单。当我点击城市时。我正在设置一个名为 scity 的 cookie。

$("#cuto li").click(function () {
autoValuenew = this.id; // console.log(autoValuenew);
document.cookie = "scity=" + this.id + ";path=/;domain=" + cookieondomain;

document.getElementById('city').value = this.id;
//Here is am pass a value to city

return false;
});

之后..我可以访问 city 的值,但在 html 源代码中看不到该值。

当我将字段类型文本更改为隐藏类型时,我可以看到 html 源代码中的值。

我不明白这两种类型是怎么回事。或者如果我在做某事,请告诉我哪里做错了。或者是否有其他方法可以做到这一点。

最佳答案

请查看这段代码,希望对您有所帮助

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>

<input type="text" id="city" class="i-p-c" value="" autocomplete="off" placeholder="Select City"/>
<ul id="cuto">
<li type="button" id="cuto">test</li>
</ul>

这是你的 JQuery

$("#cuto li").click(function () {
debugger;
autoValuenew = this.id; // console.log(autoValuenew);
$.cookie("scity", "test value"); // Setting cookie value
//document.cookie = "scity=" + this.id + ";path=/;domain=" + cookieondomain;

document.getElementById('city').value = this.id;
//Here is am pass a value to city

return false;
});

关于javascript - 如何通过javascript将值传递到输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45210072/

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