简体   繁体   English

将输入值属性设置为带空格的变量。

[英]Setting the Input Value attribute to a variable with space.

I am trying to set the value of a input button to a string variable.ie"A Guide to the School Bus"; 我正在尝试将输入按钮的值设置为字符串变量。即“校车指南”; But when the HTML loads up only the first word comes up in the button. 但是,当HTML加载完毕时,按钮中只会出现第一个单词。 My code is given below. 我的代码如下。 Thanks for the help. 谢谢您的帮助。

var title="A Guide to the School Bus";
var htmlString= "<div class="+title+ ">"+"<input type="+"button  "+"value="+title+"  onclick=loadBook()>"+"</div>";
$(htmlString).appendTo(attachPoint);

And the attachpoint is a reference in the HTML that i got using the following. 附加点是我使用以下内容获取的HTML中的引用。

var attachpoint=document.querySelector('.buttonAttachPoint');

The problem is because you're not putting quotes around the attribute values. 问题是因为您没有在属性值两边加上引号。 Try this: 尝试这个:

var htmlString= '<div class="'+title+'"><input type="button" value="'+title+'"  onclick="loadBook()"></div>';

You can either escape all the " in your string or, like I have done, just switch between ' and " . 您可以转义字符串中的所有" ,或者像我所做的那样,仅在'"之间切换。 " will show up as a normal character and ' is used to mark the start and finish of strings. "将显示为常规字符,而'用于标记字符串的开始和结束。

As a side point you probably wouldn't want to put the variable title as the class on the div as it would add each separate word as a class, so in your example the div would have 6 classes added to it. 附带一提,您可能不想将变量title作为class放在div上,因为它将每个单独的单词添加为一个类,因此在您的示例中div将添加6个类。

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

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