【问题标题】:how to send string parameter to javascript function [duplicate]如何将字符串参数发送到javascript函数[重复]
【发布时间】:2014-07-09 21:57:10
【问题描述】:

我正在尝试将字符串值传递给 javascript 函数,但它显示语法错误。我的代码在下面

product_row +='<td><input type="text" name="edit_product_type" 
id="edit_product_type_'+this.id_sale_order+'" value="'+this.product_type+'" 
onblur="updateProduct(\'' + this.id_sale_order + '\','edit');" rel="edit" />';

错误

SyntaxError: missing ; before statement
..." onblur="updateProduct(\'' + this.id_sale_order + '\','edit');" rel="edit" />'

【问题讨论】:

  • 试试这个var a = 'ppppppppppp'; var product_row ='&lt;td&gt;&lt;input type="text" name="edit_product_type" id="edit_product_type_' + a + '" value="' + a + '" onblur="updateProduct(\'' + this.id_sale_order + '\',\'edit\');" rel="edit" /&gt;';

标签: javascript string function parameters


【解决方案1】:

检查错别字:

要么:onblur="updateProduct(\'' + this.id_sale_order + '\',\''+edit+'\');"

或:onblur="updateProduct(\'' + this.id_sale_order + '\',\'edit\');"

取决于编辑是作为变量名还是字符串

【讨论】:

    【解决方案2】:

    您需要在 onblur 函数中输入正确的单引号,例如,

    onblur="updateProduct(\"' + this.id_sale_order + '\",\"edit\");" rel="edit" />
    

    您可以同时使用单引号和双引号,但请确保您使用它们时要小心

    【讨论】:

      【解决方案3】:

      你缺少引号

          product_row +='<td><input type="text" name="edit_product_type" 
      id="edit_product_type_'
      +this.id_sale_order
      +'" value="'
      +this.product_type
      +'" onblur="updateProduct(\'' 
      + this.id_sale_order 
      + '\',\'edit\');" rel="edit" />';
      

      【讨论】:

        【解决方案4】:

        阅读this的答案。

        这是一个例子:

        '<input type="button" onClick="gotoNode(\'' + result.name + '\')" />'
        

        在您的情况下,您可能希望将edit 作为字符串传递,因此请更改此

        product_row +='<td><input type="text" name="edit_product_type" 
        id="edit_product_type_'+this.id_sale_order+'" value="'+this.product_type+'" 
        onblur="updateProduct(\'' + this.id_sale_order + '\','edit');" rel="edit" />';
        

        到这里

        product_row +='<td><input type="text" name="edit_product_type" 
        id="edit_product_type_'+this.id_sale_order+'" value="'+this.product_type+'" 
        onblur="updateProduct(\'' + this.id_sale_order + '\',\'edit\');" rel="edit" />';
        

        使用转义反斜杠的地方。

        【讨论】:

          【解决方案5】:

          如果你分解你的代码,你会在倒数第二行发现问题,如果你删除“编辑”这个词,你不会得到错误,如果你把它放在双引号中,你不会得到错误,但是如果你把它放在单引号里,你会得到错误

          product_row +='<td><input type="text" 
          name="edit_product_type" 
          id="edit_product_type_'+this.id_sale_order+'"
          value="'+this.product_type+'" 
          onblur="updateProduct(\'' + this.id_sale_order + '\',
          

          \'edit\');" // 问题就在这里,所以把'edit'加上\'edit\'

          rel="edit" />';
          

          【讨论】:

            猜你喜欢
            • 2019-11-17
            • 1970-01-01
            • 1970-01-01
            • 2011-10-14
            • 2019-12-04
            • 1970-01-01
            • 2011-12-27
            • 1970-01-01
            • 2012-11-23
            相关资源
            最近更新 更多