要在html代码中恢复的Javascript中的变量

要在html代码中恢复的Javascript中的变量,javascript,html,Javascript,Html,我不确定这是否可行,但在用户输入一些文本后,我得到了一个变量。这个变量是在Javascript中获得的,但稍后我需要在代码的HTML body标记中使用它。有办法做到这一点吗 这是Javascript代码: <script type="text/javascript"> window.alert("Documento ha sido adjuntado!") <!-- var name = prompt("Referencia del Documento: ", "");

我不确定这是否可行,但在用户输入一些文本后,我得到了一个变量。这个变量是在Javascript中获得的,但稍后我需要在代码的HTML body标记中使用它。有办法做到这一点吗

这是Javascript代码:

<script type="text/javascript">
 window.alert("Documento ha sido adjuntado!")
 <!--
 var name = prompt("Referencia del Documento: ", "");
 //-->
 window.close();
 </script>

window.alert(“Documento ha sido adjuntado!”)
window.close();

您可以在页面中包含一个隐藏元素,并在隐藏值中设置所需的值,然后在需要重用它时,只需引用隐藏字段即可

HTML:

<input type="hidden" id="nameValue" />
 <script type="text/javascript">

 window.alert("Documento ha sido adjuntado!")

 var name = prompt("Referencia del Documento: ", "");

 //Sets the hidden value with "name"
 document.getElementById('nameValue').value= name;

 window.close();
 </script>
 <script type="text/javascript">

 window.alert("Documento ha sido adjuntado!")
 var name = prompt("Referencia del Documento: ", "");
 window.close();
 UpdateDatabase(name);

 function UpdateDatabase(yourString)
 {
     //Servlet call to Update Database with "yourString"
 }

 </script>

Javascript:

<input type="hidden" id="nameValue" />
 <script type="text/javascript">

 window.alert("Documento ha sido adjuntado!")

 var name = prompt("Referencia del Documento: ", "");

 //Sets the hidden value with "name"
 document.getElementById('nameValue').value= name;

 window.close();
 </script>
 <script type="text/javascript">

 window.alert("Documento ha sido adjuntado!")
 var name = prompt("Referencia del Documento: ", "");
 window.close();
 UpdateDatabase(name);

 function UpdateDatabase(yourString)
 {
     //Servlet call to Update Database with "yourString"
 }

 </script>

window.alert(“Documento ha sido adjuntado!”)
var name=prompt(“referencea del Documento:,”);
//使用“名称”设置隐藏值
document.getElementById('nameValue')。value=name;
window.close();
根据您的具体问题:

<input type="hidden" id="nameValue" />
 <script type="text/javascript">

 window.alert("Documento ha sido adjuntado!")

 var name = prompt("Referencia del Documento: ", "");

 //Sets the hidden value with "name"
 document.getElementById('nameValue').value= name;

 window.close();
 </script>
 <script type="text/javascript">

 window.alert("Documento ha sido adjuntado!")
 var name = prompt("Referencia del Documento: ", "");
 window.close();
 UpdateDatabase(name);

 function UpdateDatabase(yourString)
 {
     //Servlet call to Update Database with "yourString"
 }

 </script>

window.alert(“Documento ha sido adjuntado!”)
var name=prompt(“referencea del Documento:,”);
window.close();
更新数据库(名称);
函数UpdateDatabase(yourString)
{
//用“yourString”更新数据库的Servlet调用
}

在提示输入值后,尝试使用JavaScript更新HTML元素

 var name = prompt();
 document.getElementById("MyElement").value = name;



<input type='text' id="MyElement" />
var name=prompt();
document.getElementById(“MyElement”).value=name;

是的,但我不知道你想做什么。你能提供一个逐步的过程来解释你想要什么吗?您想让JavaScript将变量插入
标记中的某个位置,还是插入
body
元素中的某个元素/节点中?David,我需要使用servlet将来自JavaScript的字符串插入mysql数据库。我需要一些东西,我可以声明一个String=“something”,并且“something”是来自Javascript的值。感谢Rionmonster,但我需要直接使用字符串,您给我的选项是通过在HTML中使用表单。。。假设我想使用字符串将其插入mySQL数据库中的表中。。。如何使用此值?有没有一种方法可以说:String=namevalue;如果在表单中包含隐藏值,则在提示中设置该值后,将在表单中设置该值。因此,当您提交表单时,它应该具有您在提示时设置的值。这很清楚,我理解,问题是我不想使用表单。基本上,我希望javascript提示符允许用户输入文档的参考号,但在同一代码中,我有一个jsp标记,可以将值插入mysql。如果我使用表单,我需要另一个代码来处理这个问题。你看到我想做什么了吗?我相信我理解了,所以你有一个单独的函数,你需要将值插入到数据库中?如果是这种情况,您可以通过Document.getElementById('nameValue')访问存储在隐藏字段中的“文档参考号”。因此,对于DB调用,可以设置以下内容:String=document.getElementById('nameValue')。我将尝试添加一个示例Thank P.Campbell,但我需要直接使用字符串,您给我的选项是使用HTML中的表单。。。假设我想使用字符串将其插入mySQL数据库中的表中。。。如何使用此值?有没有一种方法可以说:String=namevalue;