简体   繁体   English

如何为表单中的文本字段设置初始值? 此值是从javascript函数生成的。 没有botton的情况下如何定义事件?

[英]How to set a initial value for a text field in a form? This value is generated from a javascript function. How can i define the event without botton?

<body onload="onload();">
      <input type="text" class="input-field" id="test"  title="TEST" />
</body>

This is the javascript to generate a value for the text field. 这是用于为文本字段生成值的javascript。

<script language="javascript" type="text/javascript"> 
function onload() 
{ 
 var a=0;
 a++:
 document.getElementById("test").value=a;
} 
</script>

You can use a body onload event, 您可以使用body onload事件,

http://www.w3schools.com/tags/ev_onload.asp http://www.w3schools.com/tags/ev_onload.asp

or a jquery onload event: 或jquery onload事件:

http://api.jquery.com/ready/ http://api.jquery.com/ready/

Why don´t you use one of this functions? 为什么不使用其中一个功能?

$(document).ready(function() {
   var a=0;
   a++;
   document.getElementById("test").value=a;
});

OR 要么

$(window).load(function() {
   var a=0;
   a++;
   document.getElementById("test").value=a;
});

Both of them start when the page is loaded. 两者都在页面加载时启动。

By the way... in this case a will always be 1 顺便说一句...在这种情况下, a将始终为1

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

相关问题 如何从 Javascript 设置 Angular formcontrol 字段的值? - How can I set the value of an Angular formcontrol field from Javascript? 如何使用Javascript设置没有ID的表单输入字段的值? - How to set the value of a form input field without ID using Javascript? 如何更改JavaScript的初始值 - How can I change the initial value on JavaScript JavaScript 将表单字段的初始值设置为另一个字段的相同值? - JavaScript set a form fields initial value, to the same value of another field? 如何在文本字段输入上执行javascript函数并用新值更新字段 - How can I execute a javascript function on text field input and update the field with new value 我如何在不提交表单的情况下将文本字段值传递给php变量? - How can i pass the text field value to php variable without submiting form? 如何在不提交表单的情况下获取javascript中输入字段的值? - How can I get the value of an input field in javascript without submitting a form? 如何使用javaScript正确设置文本字段的值? - How do I properly set the value of a text field with javaScript? 我如何在php中获取表单字段的值(字段是通过javascript创建的)? - how can i get the value of the field of the form (field is created from javascript) in php? JavaScript - 如何从函数传递/存储值以形成隐藏字段? - JavaScript - How to pass/store value from function to form hidden field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM