繁体   English   中英

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

[英]How to Pass a value to input field through javascript

我的问题:在输入框中显示一个由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源代码中看不到该值。

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

我不明白这两种类型将如何处理。 或者如果我在做某事,请告诉我我在做错什么。 或是否有其他方法可以做到这一点。

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

<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;
});

 element.setAttribute('value', myValue); I think you should not just change the value change it in by changing attribute value. 

暂无
暂无

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

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