javascript函数不工作 我目前正处于一个网站的开发当中。如果用户按下按钮,则需要调用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> 函数newProd(编号、客户、软件、硬件、名称) { //我简化了这个函数 警报('编号为:'+number+'客户为:'+customer+'软件为:'+software+'硬件为:'+hardware+'名称为:'+name)); }

javascript函数不工作 我目前正处于一个网站的开发当中。如果用户按下按钮,则需要调用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> 函数newProd(编号、客户、软件、硬件、名称) { //我简化了这个函数 警报('编号为:'+number+'客户为:'+customer+'软件为:'+software+'硬件为:'+hardware+'名称为:'+name)); },javascript,html,google-chrome,alert,Javascript,Html,Google Chrome,Alert,当用户按下Internet explorer中的按钮时,该功能工作正常!不幸的是,该功能在Chrome或Safari中不起作用 有人知道出了什么问题吗?表单字段不应该定义为全局变量。也许在IE中是这样,但这不是你可以依赖的行为。试试这个: onClick="newProd('a number', this.form.textfieldCustomer.value, this.form.textfieldSoftware.value, this.form.textfield

当用户按下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)">
当然,还要添加一个表单来包装输入


演示:

表单字段不应定义为全局变量。也许在IE中是这样,但这不是你可以依赖的行为。试试这个:

onClick="newProd('a number',
    this.form.textfieldCustomer.value,
    this.form.textfieldSoftware.value,
    this.form.textfieldHardware.value,
    this.form.textfieldDescription.value)">
当然,还要添加一个表单来包装输入


演示:

在我看来,您的代码中有两个大错误。首先,对输入字段的访问是错误的。它需要连接到一个实例变量,比如“document”。第二个,表单标签丢失。如果将输入字段包装到表单标记中,则可以访问Esailija发布的值

在我看来,您的代码中有两个大错误。首先,对输入字段的访问是错误的。它需要连接到一个实例变量,比如“document”。第二个,表单标签丢失。如果将输入字段包装到表单标记中,则可以访问Esailija发布的值

您了解jQuery吗?使用
onclick
属性被认为是不好的做法,因为它会将Javascript逻辑放入HTML标记中。您应该使用纯Javascript设置事件处理。这里描述了更详细的原因:您了解jQuery吗?使用
onclick
属性被认为是不好的做法,因为它将Javascript逻辑放入HTML标记中。您应该使用纯Javascript设置事件处理。更详细的原因如下: