简体   繁体   English

HTML图片标记中的onclick-将字符串值传递给javascript函数

[英]onclick in html image tag - pass string value to javascript function

Code: 码:

// inside for loop.. //在for循环内

$("#tabs").append('<li><a href="#tab' + 1 + '">' + 1 + '</a><img src="/Images/pdf.png" onclick="test('1s')" /></li>');

function test(val) {
    alert(val);
    //return false;
}

Please see this code onclick="test('1s')" . 请查看此代码onclick="test('1s')" Is there any syntax wrong?. 有语法错误吗? I am not able to pass the value 1s to the test function. 我无法将值1s传递给test功能。

But I can pass the value if it is 1 instead of 1s . 但是我可以传递值,如果它是1而不是1s

UPDATE : 更新

How to pass a variable value...Lets say 1s is in variable val .. If I put onclick="test(\\'val\\')" , I am getting the val as "val" instead of 1s 如何传递变量值...假设1s在变量val 。如果我将onclick="test(\\'val\\')"放进去,我将val改为“ val”而不是1s

SOLUTION: 解:

I solved myself..Refer to https://stackoverflow.com/a/32945494/5409139 我解决了自己.. 参阅https://stackoverflow.com/a/32945494/5409139

您应该转义单个qoutes: '带有\\符号。

$("#tabs").append('<li><a href="#tab' + 1 + '">' + 1 + '</a><img src="/Images/pdf.png" onclick="test(\'1s\')" /></li>');

The code is in a single-quoted string argument, so the '' around 1s take you out of the string and back to the argument of the append function call. 该代码是在一个单引号字符串参数,所以''周围1s带你出去的字符串和回的论点append函数调用。 Try test(\\'1s\\') (backslashes before each quote mark) instead. 尝试使用test(\\'1s\\') (每个引号之前的反斜杠)。

More details of JavaScript strings are, eg, here . JavaScript字符串的更多详细信息在例如此处

This is simple bug. 这是简单的错误。 Please do following... 请执行以下操作...

$("#tabs").append("<li><a href='#tab" + 1 + "'>" + 1 + "</a><img src='/Images/pdf.png' onclick=\"test('1s')\" /></li>");

function test(val) {
    alert(val);
}

I got the solution. 我找到了解决方案。 I added the quotes accordingly. 我相应地添加了引号。

var val = "1s";
$("#tabs").append('<li><a href="#tab' + 1 + '">' + 1 + '</a><img src="/Images/pdf.png" onclick="test(\'' + val + '\')" /></li>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用图像标签通过“onclick”传递 function - Using image tag to pass a function with 'onclick' 将数组传递给锚标记onclick上的javascript函数 - Pass an array to javascript function on anchor tag onclick 在 ZD52387880E1EA22817A72D375Z21 中使用 Javascript function 将动态值传递给 HTML 标签 - Pass dynamic value to HTML tag using Javascript function in Java 就像我们在html的onclick事件监听器中一样,如何在Javascript的onClick事件监听器中传递字符串值 - How to pass string value in onClick event listener in Javascript as we do it in onclick event listener in html 如何在onclick函数html中传递字符串 - how to pass string in onclick function html 如何通过使用MVC单击图像从锚标记传递ID值onclick函数? - how to pass ID value onclick function from anchor tag by click on image using MVC? 将名称和值传递给JavaScript onClick函数? - Pass the name and value to JavaScript onClick function? HTML5视频标记:Onclick PHP函数无法访问JavaScript:转义字符串 - HTML5 video Tag: Onclick PHP function not getting to the JavaScript: Escaping String 如何将字符串值从html作为函数参数传递给javascript? - how to pass string value to javascript as function parameter from html? 如何将 HTML 输入字符串值作为 JavaScript Function 参数传递? - How to pass HTML input string value as a JavaScript Function Parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM