【问题标题】:How can I replace one Placeholder Word?如何替换一个占位符词?
【发布时间】:2014-09-08 12:09:56
【问题描述】:

我有一个占位符,它被命名为 {{inactive}}

{{inactive}} 在我的 HTML 中是一个复选框。

它有2个输出,一个输出是“true”,另一个输出是“false

现在我将替​​换它,因为当我执行 wkhtmltoPdf.exe 任务时,我的 PDF 中的单词是真还是假,但我需要:not given or are given。

这是一个示例,但它不起作用:

<script>
    inact = true;
</script>
<tr style="height:58pt">
    <td id="test" align="right">
        <script type="text/javascript">
            document.write(inact)
        </script>
    </td>
</tr>

function myFunction() {
    var testing;
    if (inact = true) {
        testing = "are given";
    } else {
        testing = "not given"; //else = not true (false) 
    }
    document.getElementById("test").innerHTML = testing;
}

【问题讨论】:

  • "指向 jsfiddle.net 的链接必须附有代码。<...>" 请将您的代码放在问题本身中。不要通过在代码块中放置 jsfiddle 链接来欺骗系统。您收到的警告消息是有原因的。
  • 完成,我已经把代码放在这里了。
  • 保留小提琴链接,现场演示总是有帮助的

标签: javascript jquery html pdf wkhtmltopdf


【解决方案1】:
  • 函数周围缺少脚本标签
  • if 语句中的单个等于
  • 您没有在任何地方调用该函数

以下是您的代码的调整版本:

<tr style="height:58pt">
    <td id="test" align="right"></td>
</tr>

<script>
    inact = true;

    function myFunction() {
        var testing;
        if (inact == true) {
            testing = "are given";
        } else {
            testing = "not given";
        }
        document.getElementById("test").innerHTML = testing;
    }

    myFunction();
</script>

【讨论】:

  • 我需要这个用于 而不是跨度,我无法在其中设置 ID
  • 好的,我已将 span 换回您的原始代码。只要 ID 相同,元素是什么都无关紧要。 还需要在 中才能正确呈现。
  • 正如我所说,您需要将
  • 包装在
    中以使其呈现。 jsfiddle.net/hoLnmbtv/2
【解决方案2】:

你必须这样做:

你的 HTML 应该是:

<script>
 inact = false;
</script>
<body onload="myFunction();">
<table>
<tr style="height:58pt">
  <td id="test" align="right">
    <script type="text/javascript">
        document.write(inact)
    </script>
</td>
</tr>
</table>    
</body>

你的 JS 应该是:

function myFunction() {
var testing;
if (inact == true) {
    testing = "are given";
} else {
    testing = "not given"; //else = not true (false) 
}
document.getElementById("test").innerHTML = testing;
}

现在您将得到正确的结果。

【讨论】:

  • if 语句中只有一个等号,这意味着它永远为真。
  • @FlabbyRabbit 是的,我是正确的,我在打字时忘记了。谢谢
猜你喜欢
相关资源
最近更新 更多
热门标签