javascript 在javascript的onclick函数中将参数作为包含空格的字符串传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30961170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing parameter as a string containg white spaces in javascript's onclick function
提问by Gurpinder
"<button onclick=editRecords('" + partId + "')>Add Quantity</button>";
If I pass the partId
string without spaces, it's working fine. But if I pass 'MRF 01'
, it gives me
如果我传递partId
没有空格的字符串,它工作正常。但是如果我通过了'MRF 01'
,它会给我
Uncaught SyntaxError: Unexpected token ILLEGAL.
未捕获的语法错误:意外标记非法。
Why is this and how can I fix that?
为什么会这样,我该如何解决?
回答by Unni Babu
Use
whenever you pass space as a function parameter. If you are using js to generate onclick, use encodeURIComponent.
每当您将空格作为函数参数传递时使用。如果使用js生成onclick,使用encodeURIComponent。
"<button onclick=editRecords('" + encodeURIComponent(partId) + "')>Add Quantity</button>";
回答by leaf
From the poster himself.It's a primarily opinion based answer, there is nothing conventional in using single quotes (I have discovered C syntax and now I prefer double quotes...).
来自海报本人。这是一个主要基于意见的答案,使用单引号没有什么传统的(我发现了 C 语法,现在我更喜欢双引号......)。
I would rather change the organisation of quotes in order to get a cleaner code. This will makes it more readable as well as more conventional (in my point of view):
我宁愿更改引号的组织以获得更清晰的代码。这将使其更具可读性和更传统(在我看来):
var msg = 'Sometimes SO looks like a McDonalds drive through.';
var html = '<button onclick="clickHandler(\'' + msg + '\')">click me</button>';
function clickHandler (msg) { alert(msg); }
document.body.innerHTML = html;
Even more conventional would be to follow Felix Kling's wisdom(last line).
更传统的是遵循Felix Kling 的智慧(最后一行)。
回答by Felix Kling
If an HTML attribute value is supposed to contain a space, then you have to use quotation marks to delimit the value.
如果 HTML 属性值应该包含空格,则必须使用引号来分隔该值。
Pay attention to the syntax highlight here:
注意这里的语法高亮:
<span foo=bar baz ></span>
This is an attribute foo
with value "bar"
and a boolean attribute baz
.
这是一个foo
带有 value"bar"
和 boolean 属性的属性baz
。
On the other hand, this
另一方面,这
<span foo="bar baz"></span>
is an attribute foo
with value "bar baz"
. Notice how baz
is highlighted differently?
是一个foo
具有 value的属性"bar baz"
。请注意如何baz
以不同的方式突出显示?
To fix your issue, either put quotation marks around the value of onclick
, or better, use a different way to bind the event handler.
要解决您的问题,请在 的值周围加上引号onclick
,或者更好的是,使用不同的方式绑定事件处理程序。
回答by hardika
I would suggest that this is the best option. You can set any attribute you want.
我建议这是最好的选择。您可以设置所需的任何属性。
var e = document.createElement("span");
e.className = "close-li";
e.setAttribute('aria-hidden', 'true');
e.setAttribute('data-toggle', 'modal');
e.setAttribute('data-target', '#CreateModal');
e.setAttribute("onClick", "FunctionName(" + parameters + ");" );
e.innerText = "×";
$('#id').append(e); //Id would be the place where you want to append. I used jquery so, I used append
回答by Kusal Thiwanka
String parameters with white space
带空格的字符串参数
<input type="button" onClick="return myFunction('10', 'your text hear')" />
or
PHP string variables with white space
或
带有空格的 PHP 字符串变量
<input type="button" onClick="return myFunction('<?php echo $id ?>', '<?php echo $designation ?>')" />
JS Function
JS函数
function setVisibility(id, designation) {...your js code hear...}