尝试通过javascript提交html表单变量时没有任何反应
Nothing happens when trying to submit html form variable via javascript
我是一个初学者javascript用户,我正在尝试让这个html表单传递给这个javascript函数并输出name变量,但是当我单击按钮提交时发生的一切是表单输入清除并且它停留在表单上而不是传递给javascript。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Life Insurance Calculator</title>
<script type="text/javascript">
var title = "Welcome to Life Insurance Co..!";
//Declaring variable title
document.writeln(title.fontsize(8).fontcolor('red'));
//Printing out the title on the page
function Name(form){
var customername = document.getElementByID("name").value;
document.write(customername);
}
</script>
</head>
<body>
<form name = "LifeCalcForm" action="" method="get">
<p>To get a life insurance quote, please tell us about yourself.</p>
<p><i>Note:only those under the age of 80 and non-smokers may apply</i></p>
<p>Enter your name: <input type="text" name="name" /></p>
<input type="submit" value="Calculate" onClick="Name(this.form)">
</form>
</body>
</html>
当您在
页面加载后(特别是在加载事件之后(执行document.write(customername);
时,它将首先清除整个页面(包括脚本(并写入新内容。
而是执行以下操作:
alert(customername);
清除警报后(例如单击"确定"(,表单将提交,页面将重新加载。
顺便提一下,由于您在调用中传递了对函数的引用,并且窗体控件具有名称,因此可以使用:
alert(form.name.value);
请注意,窗体控件名称和 ID 用于创建窗体对象的属性,因此,名称为"name"的窗体控件将覆盖窗体自己的 name 属性(窗体还具有提交、操作、元素、重置等属性(。因此,切勿使用可能与标准表单属性名称相同的控件名称,而应使用"用户名"等内容。
按照惯例,变量名称是为构造函数保留的,因此对普通函数使用小写。此外,函数是做事,所以通常最好使用一个动词来描述它的作用,例如
function getUserName(form) {
return form.userName.value;
}
试试这个:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Life Insurance Calculator</title>
<script type="text/javascript">
var title = "Welcome to Life Insurance Co..!";
// Declaring variable title
document.writeln(title.fontsize(8).fontcolor('red'));
// Printing out the title on the page
function Name(){
var customername = document.getElementById("name").value;
document.write(customername);
}
</script>
</head>
<body>
<form name = "LifeCalcForm" action="" method="get">
<p>To get a life insurance quote, please tell us about yourself.</p>
<p><i>Note:only those under the age of 80 and non-smokers may apply</i></p>
<p>Enter your name: <input type="text" id="name" name="name" /></p>
<input type="button" value="Calculate" onClick="Name()">
</form>
</body>
</html>
相关文章:
- 函数参数中的数据与指定变量之间的任何性能差异
- 如何获取保存在变量中的HTML代码的任何属性的值
- 使用jQuery获取任何变量的输入值
- 当输入为变量时,.replace.不执行任何操作
- 我可以't克服这个错误“;未定义的局部变量或方法`f'"任何原因
- 我使用谷歌标签管理器在我的网站上启动标签.变量“;未定义的“;在任何版本的Internet Explorer的自定义代码
- 查找任何未定义的变量并从url字符串中隐藏
- 确定是否在不使用任何全局变量的情况下加载了窗口
- 是否有任何方法可以访问外部范围's变量
- jQuery,如何测试一个变量是一个文本节点,不包含任何标记
- 如何将变量设置为给定时刻数组的值,而不是数组的任何值
- q 从 .then 函数分配给更高级别的作用域变量是否存在任何陷阱
- 如何从NodeJsServer的查询字符串中获取id或任何变量数据
- 如何将“n”定义为任何自然基数变量
- PHP 反序列化 JS 序列化的变量字符串
- 对角度模块使用单个全局变量是否是一种反模式
- 是否有任何工具可以将 JavaScript 返工为每个函数表单一个变量
- 单击新插入的变量块中的关闭按钮时没有任何反应
- 全局名称空间变量:任何备选方案
- AJAX没有在服务器上传递变量(任何AJAX)