简体   繁体   English

在javascript的onclick函数中将参数作为包含空格的字符串传递

[英]Passing parameter as a string containg white spaces in javascript's onclick function

"<button onclick=editRecords('" + partId + "')>Add Quantity</button>";

If I pass the partId string without spaces, it's working fine.如果我传递没有空格的partId字符串,它就可以正常工作。 But if I pass 'MRF 01' , it gives me但是如果我通过'MRF 01' ,它会给我

Uncaught SyntaxError: Unexpected token ILLEGAL.未捕获的语法错误:意外标记非法。

Why is this and how can I fix that?为什么会这样,我该如何解决?

Use &nbsp; 使用&nbsp; 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>";

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的智慧 (最后一行)。

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 . 这是一个值为"bar"的属性foo和一个布尔属性baz

On the other hand, this 另一方面,这个

<span foo="bar baz"></span>

is an attribute foo with value "bar baz" . 是一个值为"bar baz"的属性foo Notice how baz is highlighted differently? 请注意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的值周围加上引号,或者更好, 使用不同的方式绑定事件处理程序

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

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...}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM