如何将 JS 变量传递到“value=”变量中INPUT 语句的参数

发布于 2024-10-09 19:49:28 字数 594 浏览 2 评论 0原文

下面的示例应该将 JS 中的值显示为 INPUT 语句中的值,但没有。

<html>
<body>

<script type="text/javascript">
var qty = 1;
if (qty<3)
{
var shipping=1.92;
var shp_method="USPS First Class Package";
}
else if (qty>=3 && qty<14)
{
var shipping=4.85;
var shp_method="USPS Small Flat Rate Package";
}
else
{
var shipping=10.82;
var shp_method="USPS Medium Flat Rate Package";
}
</script>
<input type="text" name="shp_method" value="<?=shp_method;?>" TEXT STYLE="text-align:center" readonly="readonly" size="30">
</body>
</html>

The example below should display the value from the JS as the value in the INPUT statement but does not.

<html>
<body>

<script type="text/javascript">
var qty = 1;
if (qty<3)
{
var shipping=1.92;
var shp_method="USPS First Class Package";
}
else if (qty>=3 && qty<14)
{
var shipping=4.85;
var shp_method="USPS Small Flat Rate Package";
}
else
{
var shipping=10.82;
var shp_method="USPS Medium Flat Rate Package";
}
</script>
<input type="text" name="shp_method" value="<?=shp_method;?>" TEXT STYLE="text-align:center" readonly="readonly" size="30">
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归属感 2024-10-16 19:49:28

您正在使用的语法:

value="<?=shp_method;?>"

仅对 PHP 有效。

如果你想使用 javascript,你可以这样做:

document.getElementById('#textbodID').value = shp_method;

你的代码应该是这样的:

<script type="text/javascript">
window.onload = function(){
    var qty = 1;
    if (qty<3)
    {
    var shipping=1.92;
    var shp_method="USPS First Class Package";
    }
    else if (qty>=3 && qty<14)
    {
    var shipping=4.85;
    var shp_method="USPS Small Flat Rate Package";
    }
    else
    {
    var shipping=10.82;
    var shp_method="USPS Medium Flat Rate Package";
    }

        document.getElementById('#textbodID').value = shp_method;
};
</script>

其中 textbodID 被假定为你的输入字段的 id ,它可以像这样分配:

<input id="textbodID" />

The syntax you are using:

value="<?=shp_method;?>"

is valid only for PHP.

If you want to do with javascript, you can do something like this:

document.getElementById('#textbodID').value = shp_method;

Your code should be like this:

<script type="text/javascript">
window.onload = function(){
    var qty = 1;
    if (qty<3)
    {
    var shipping=1.92;
    var shp_method="USPS First Class Package";
    }
    else if (qty>=3 && qty<14)
    {
    var shipping=4.85;
    var shp_method="USPS Small Flat Rate Package";
    }
    else
    {
    var shipping=10.82;
    var shp_method="USPS Medium Flat Rate Package";
    }

        document.getElementById('#textbodID').value = shp_method;
};
</script>

Where textbodID is assumed to be the id of your input field which can be assigned like this:

<input id="textbodID" />
爱情眠于流年 2024-10-16 19:49:28

尝试这样:

<input type="text" name="ye" id="yea"> //input box 

        <script type="text/javascript">
        var mytime=new Date();
        document.getElementById("yea").value=(mytime.getFullYear());
</script>
<input type="submit" value="Submit" name="submit">// use submit button to get input value

然后在同一个jsp页面中使用scriplet标签:

像这样:

<%  String y=request.getParameter("submit"); 
   if(y.equals("Submit"))
   {
    String s=request.getParameter("ye");
    System.out.println(s);
   }
%>

单击提交后,您可以在控制台中看到javascript值。
然后将此字符串值保存在数据库中
然后你就可以在任何地方访问这个字符串了。
请不要复制并粘贴此示例。只是尝试在您的页面中写入以避免错误。

try like this:

<input type="text" name="ye" id="yea"> //input box 

        <script type="text/javascript">
        var mytime=new Date();
        document.getElementById("yea").value=(mytime.getFullYear());
</script>
<input type="submit" value="Submit" name="submit">// use submit button to get input value

then use scriplet tag in same jsp page:

like this:

<%  String y=request.getParameter("submit"); 
   if(y.equals("Submit"))
   {
    String s=request.getParameter("ye");
    System.out.println(s);
   }
%>

after click submit you can see javascript value in your console.
then save this string value in your database
and then u can access this String everywhere.
please don't copy and paste this example. just try to write in your page to avoid errors.

垂暮老矣 2024-10-16 19:49:28

首先向输入添加一个 id 属性。
这将在页面加载后自动设置该值:

window.onload = function (){
var qty = 1;
if (qty<3)
{
var shipping=1.92;
var shp_method="USPS First Class Package";
}
else if (qty>=3 && qty<14)
{
var shipping=4.85;
var shp_method="USPS Small Flat Rate Package";
}
else
{
var shipping=10.82;
var shp_method="USPS Medium Flat Rate Package";
}
document.getElementById('inputId').value = shp_method;
}

First of all add an id attribute to the input.
this will automatically set the value as soon as the page loads:

window.onload = function (){
var qty = 1;
if (qty<3)
{
var shipping=1.92;
var shp_method="USPS First Class Package";
}
else if (qty>=3 && qty<14)
{
var shipping=4.85;
var shp_method="USPS Small Flat Rate Package";
}
else
{
var shipping=10.82;
var shp_method="USPS Medium Flat Rate Package";
}
document.getElementById('inputId').value = shp_method;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文