简体   繁体   English

如何将Javascript变量放入html页面

[英]How to put Javascript variable into an html page

I got this line in html : 我在html中得到了这一行:

<input type="text" name="number" value= "" style="width: 20px;border:#FFFFFF none 0px;" readonly></p>

And I also have a variable in a function in javascript that I would like to put in the value of the html line. 而且我在JavaScript函数中也有一个变量,希望将其放入html行的值中。

http://jsfiddle.net/6v28hj75/1/ http://jsfiddle.net/6v28hj75/1/

function myFunction() {
input = document.getElementsByTagName('input')[0];
input.value = "Your value here"; 
}
myFunction()

I made you a simple example, pure Javascript. 我为您提供了一个简单的示例,纯Javascript。

For this purpose you can use jQuery like this : 为此,您可以像这样使用jQuery:

 jQuery(function($){ var foo = 'bar'; $('input[name="number"]').val(foo); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="number" value=""> 

You can access JavaScript variables in JSP as below: 您可以按以下方式在JSP中访问JavaScript变量:

JavaScript: JavaScript:

 var fruit = "Apple";
 $("#someTextBox").val(fruit);

JSP: JSP:

<input type="text" id ="someTextBox" name="number" style="width: 20px;border:#FFFFFF none 0px;"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM