admin管理员组文章数量:1302275
I am pulling a cookie value into a hidden form field. If there is a name I know how to do it but when there is not I cannot seem to figure it out. I cannot change the form by adding anything to the input field but need to save a cookie value to that field.
Example:
<p class="form-field anic_source pd-hidden hidden">
<input type="hidden" value="">
</p>
<script>
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
document.querySelector("input[name='fieldname']").value = getCookie("anic_source");
</script>
If the form had a name this works great but without one it does nothing. Maybe somehow basing it off of the child of anic_source paragraph but I an still relatively green at js and not sure how that would work.
I am pulling a cookie value into a hidden form field. If there is a name I know how to do it but when there is not I cannot seem to figure it out. I cannot change the form by adding anything to the input field but need to save a cookie value to that field.
Example:
<p class="form-field anic_source pd-hidden hidden">
<input type="hidden" value="">
</p>
<script>
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
document.querySelector("input[name='fieldname']").value = getCookie("anic_source");
</script>
If the form had a name this works great but without one it does nothing. Maybe somehow basing it off of the child of anic_source paragraph but I an still relatively green at js and not sure how that would work.
Share asked Feb 10 at 14:22 Brandon OrndorffBrandon Orndorff 33 bronze badges 3 |1 Answer
Reset to default 0Alternatively, you could use the id-attribute or any data-* attribute to select the hidden inputfield, e.g. <input type="hidden" data-id="myHiddenField" />
and then select it via document.querySelector( '[data-id="myHiddenField"]' )
.
本文标签: javascriptSave Cookie value to hidden input field without name or idStack Overflow
版权声明:本文标题:javascript - Save Cookie value to hidden input field without name or id? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741711930a2393894.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
document.querySelector(".anic_source input[type='hidden']")
...? // This will of course only achieve anything, if the hidden field gets read by some other JS later on. With a normal form submission, this value would not be included in the first place, if the input field doesn't have aname
. – C3roe Commented Feb 10 at 14:35