我对 HTML
和 jquery
完全陌生,我已经计算了 jquery
部分中的一些值以及我想要传递给 HTML
。
<script>
var datetime = new Date();
var res = datetime.toString(); //res is the variable
</script> // end of the string
<div class="col-xs-8 center login-sub-container clearfix">
<form id="loginDetails" name="loginDetails" action="http://192.198.9.228/loginOnline" method="post">
<div class="login-form-elements">
<input type="hidden" value=res name="dateTime"> // here the result i want to pass
</div>
</form>
</div>
当我看到请求数据包时,我发现该值正在传递结果
,而不是该值位于结果变量内
最佳答案
这里不需要 jQuery。只需使用 .value
也不要在 html 中设置没有单引号或双引号的属性。
我的意思是那部分<input ... value=res ...>
您应该始终使用单个 '
或双 "
引号 ( <input ... value="res" ...>
)
var datetime = new Date();
var res = datetime.toString();
document.querySelector("input[name='dateTime']").value = res;
<div class="col-xs-8 center login-sub-container clearfix">
<form id="loginDetails" name="loginDetails" action="http://192.198.9.228/loginOnline" method="post">
<div class="login-form-elements">
<input type="text" value="result" name="dateTime">
</div>
</form>
</div>
关于javascript - 如何在调用post方法时将jquery变量传递给html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50856224/