Javascript将变量传递给input元素中的内联函数(Javascript passing variable to inline function in input element)

编程入门 行业动态 更新时间:2024-10-24 18:26:15
Javascript将变量传递给input元素中的内联函数(Javascript passing variable to inline function in input element)

我试图将一个对象totalArray传递给一个输入元素id = fullReport,这样我就可以在函数中使用变量totalArray:printFullReport。

输入元素连接在一个字符串中,以便它最终可以创建到一个表中。

我无法将totalArrays传入onclick =“printFullReport('+ totalArray +')”>可能是因为字符串连接,在我的控制台中我收到:“Uncaught SyntaxError:意外的标识符”,这意味着缺少; 或',但对我来说似乎很好。

所以这是我最初的尝试:

<script type="text/javascript"> console.log(totalArray); //{"1":0,"2":0,"3":54700.33,"4":54700.33,"5":0,"6":0,"7":-54700.33,"8":0,"9":0,"10":0}; var str = ""; str += '<td> <input id ="fullReport" class="button" type="button" value="Full Report" onclick="printFullReport('+ totalArray+ ')"> TOTAL (GBP):</td>'; </script>

这是我阅读并遵循最佳答案后的第二次尝试: 内嵌onclick JavaScript变量

<script type="text/javascript"> var str = ""; console.log(totalArray) //{"1":0,"2":0,"3":54700.33,"4":54700.33,"5":0,"6":0,"7":-54700.33,"8":0,"9":0,"10":0}; function init(){ document.getElementById('fullReport').onclick=function(){ printFullReport(totalArray); }; } window.onload=init; str += '<td><input id ="fullReport" class="button" type="button" value="Full Report" onclick="init();">TOTAL (GBP):</td>'; </script>

但是现在返回:未捕获的ReferenceError:init没有被定义。

在JavaScript字符串中使用html元素时,我可能不理解范围。

I am trying to pass an object, totalArray into an input element, id=fullReport so that I will be able to use the variable totalArray within a function: printFullReport.

The input element is concatenated in a string so that it can be eventually created into a table.

I am unable to pass totalArrays into onclick="printFullReport('+ totalArray+ ')"> possibly due the string concatenation, in my console I recieve: "Uncaught SyntaxError: Unexpected identifier" which implies a missing ; or ' but it appears to fine to me.

So this is my initial attempt:

<script type="text/javascript"> console.log(totalArray); //{"1":0,"2":0,"3":54700.33,"4":54700.33,"5":0,"6":0,"7":-54700.33,"8":0,"9":0,"10":0}; var str = ""; str += '<td> <input id ="fullReport" class="button" type="button" value="Full Report" onclick="printFullReport('+ totalArray+ ')"> TOTAL (GBP):</td>'; </script>

this is my second attempt after reading and following the top answer: Inline onclick JavaScript variable

<script type="text/javascript"> var str = ""; console.log(totalArray) //{"1":0,"2":0,"3":54700.33,"4":54700.33,"5":0,"6":0,"7":-54700.33,"8":0,"9":0,"10":0}; function init(){ document.getElementById('fullReport').onclick=function(){ printFullReport(totalArray); }; } window.onload=init; str += '<td><input id ="fullReport" class="button" type="button" value="Full Report" onclick="init();">TOTAL (GBP):</td>'; </script>

but now this is returning: Uncaught ReferenceError: init is not defined.

I am possibly not understanding scope when having html elements in javascript strings.

最满意答案

对于第二次尝试,您错过了结尾双引号 "

<script type="text/javascript">

你需要从totalArray可访问的地方分配onclick 。 (现在你在评论中澄清说totalArray不在全局范围内 。)

演示

<script type="text/javascript">
var str = "";
totalArray ={ "1":0,"2":0,"3":54700.33,"4":54700.33,"5":0,"6":0,"7":-54700.33,"8":0,"9":0,"10":0};

//console.log(totalArray) //{"1":0,"2":0,"3":54700.33,"4":54700.33,"5":0,"6":0,"7":-54700.33,"8":0,"9":0,"10":0};

function init(){
    document.getElementById('fullReport').onclick=function(){
        printFullReport(totalArray);
    };
}
window.onload=init;

str += '<td><input id ="fullReport"  class="button" type="button" value="Full Report" onclick="init();">TOTAL (GBP):</td>';
</script>

<input id ="fullReport"  class="button" type="button" value="Full Report" onclick="init();">TOTAL (GBP): 
  
 

For second attempt, you are missing the closing double-quote "

<script type="text/javascript">

And you need to assign the onclick from where the totalArray is accessible. (now that you clarified in comments that totalArray is not globally scoped.)

Demo

<script type="text/javascript">
var str = "";
totalArray ={ "1":0,"2":0,"3":54700.33,"4":54700.33,"5":0,"6":0,"7":-54700.33,"8":0,"9":0,"10":0};

//console.log(totalArray) //{"1":0,"2":0,"3":54700.33,"4":54700.33,"5":0,"6":0,"7":-54700.33,"8":0,"9":0,"10":0};

function init(){
    document.getElementById('fullReport').onclick=function(){
        printFullReport(totalArray);
    };
}
window.onload=init;

str += '<td><input id ="fullReport"  class="button" type="button" value="Full Report" onclick="init();">TOTAL (GBP):</td>';
</script>

<input id ="fullReport"  class="button" type="button" value="Full Report" onclick="init();">TOTAL (GBP): 
  
 

更多推荐

本文发布于:2023-07-22 18:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内联   变量   函数   元素   input

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!