【问题标题】:How to Pass a value to input field through javascript如何通过javascript将值传递给输入字段
【发布时间】:2017-07-20 09:01:22
【问题描述】:

我的问题:由 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 源代码中看到值。

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

【问题讨论】:

  • 你说的在html元素部分看不到那个值是什么意思
  • 在我的源代码@H77
  • 控制台窗口中是否出现任何错误?
  • 不.. 我没有得到@M.Nabeel

标签: javascript jquery html cookies


【解决方案1】:

请看这段代码,希望对你有帮助

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

【讨论】:

  • 谢谢@M.Nabeel
  • 很高兴能帮助 Vishal。如果有帮助,请将其标记为答案:)
  • 但我需要的是@Pratheesh 答案
【解决方案2】:

element.setAttribute('value', myValue);


I think you should not just change the value change it in by changing attribute value.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 2014-10-31
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    相关资源
    最近更新 更多