为什么不替换为单选按钮值中的整个字符串?

Why isn't this replacing with the whole string in radio button value?

本文关键字:字符串 替换 单选按钮 为什么不      更新时间:2023-09-26

我正在使用.replaceWith()来使用数组更改单选按钮的值:

$("#choice1").replaceWith("<li id = 'choice1'>" + "<input type='radio' >name='choice' value="+allQuestions[currentTurn].possibleAnswers[0]+">"+allQuestions[currentTurn].>possibleAnswers[0] "</li>");

,当我有一个字符串(例如。"The Last Battle"),第一个+allQuestions[currentTurn].possibleAnswers[0]只返回字符串的第一个单词,其中相同的代码,第二次,返回整个字符串。

谁能告诉我为什么?如何让它们都返回整个字符串?

谢谢!

如果属性值中包含空格,则需要用引号括起来。

$("#choice1").replaceWith("<li id = 'choice1'>" + 
    "<input type='radio' name='choice' value='" +
    allQuestions[currentTurn].possibleAnswers[0] +
    "'>" +
    allQuestions[currentTurn].>possibleAnswers[0] "</li>");

否则,你的HTML看起来像这样:

<input type='radio' value=The Last Battle>

从这里可以看到,值只是The,而LastBattle看起来像是额外的属性(SO的语法高亮显示甚至使这一点清楚)。