我目前正在开发一个网站。如果用户按下按钮,则需要调用 JavaScript 函数。我将此函数简化为:
<html>
<head>
<script type="text/javascript">
function newProd(number,customer,software,hardware,name)
{
//I simplified this function
alert('number is: '+number+'customer is: '+customer+' software is: '+software+' hardware is: '+hardware+' name is: '+name);
}
</script>
</head>
<body>
<input type="text" name="textfieldCustomer"><br>
<input type="text" name="textfieldSoftware"><br>
<input type="text" name="textfieldHardware"><br>
<input type="text" name="textfieldDescription"><br>
<input type="button" name="button" value="go to function" onClick="newProd('a number',textfieldCustomer.value,textfieldSoftware.value,textfieldHardware.value,textfieldDescription.value)">
</body>
当用户按下 Internet Explorer 中的按钮时,该功能完美运行!不幸的是,该功能在 Chrome 或 Safari 中不起作用。
有人知道出了什么问题吗?
最佳答案
表单字段不应定义为全局变量。也许在 IE 中是这样,但这不是您可以依赖的行为。试试这个:
onClick="newProd('a number',
this.form.textfieldCustomer.value,
this.form.textfieldSoftware.value,
this.form.textfieldHardware.value,
this.form.textfieldDescription.value)">
哦,当然还要添加一个表单来包装输入。
关于JavaScript 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11382868/