【问题标题】:To insert elements into the javascript input box将元素插入到 javascript 输入框中
【发布时间】:2021-10-17 00:59:16
【问题描述】:

我是一名学习 JavaScript 的学生。我想把strong标签里的checkValue值放到输入框里,但是不知道怎么写JavaScript才能把它放到输入框里。我需要找到 div 的 cart_table 吗?还是我应该找到一个强标签?

<script>
$().ready(function() {
   $("#optionSelect").change(function() {
       var checkValue = $("#optionSelect").val();
       var checkText = $("#optionSelect option:selected").text();
       var product = $("#productname").text();
       var price = parseInt($("#price").text());
       var test = parseInt($(this).find(':selected').data('price'));
       var hapgae = price + test

       if (checkValue != "no") {
           var whtml =
               "            <div class=\"cart_table\">\n" +
               "            <hr style='width: 300px; margin-bottom: 30px;'>" +
               "            <p style='font-size: 17px;'>" + product + "</p>" +
               "                <p style='font-size: 13px; margin-top: -10px; class=\"check\" margin-bottom: 20px;'>" + checkText + "</p>" +
               "                        <strong class=\"value_code2\">" + checkValue + "</strong>\n" +
               "                   <input name=\"value_code\" id=\"value_code\" class=\"value_code\">" +

强标签和输入标签参考此部分。

               "                        <strong class=\"value_code2\">" + checkValue + "</strong>\n" +
               "                   <input name=\"value_code\" id=\"value_code\" class=\"value_code\">" +

【问题讨论】:

    标签: javascript django django-templates


    【解决方案1】:

    首先我建议你使用反引号

    var html = `
      <div>
        <span>Some HTML here ${some_var}</span>
      </div>
    `; 
    

    这样可以节省您的时间和精力,然后您可以在任何需要的地方插入变量

    也检查这个线程

    Creating multiline strings in JavaScript

    【讨论】:

    • 谢谢,不过我只是想把checkvalue放在输入框里。
    • 你到底是什么意思?您想根据校验值上的 if 条件渲染(显示)输入,或者您将校验值变量放入输入
    • var checkText = $("#optionSelect option:selected").text();我想把这个值放在 input name="value_code" 中。
    • `&lt;input name=${checkText}&gt;`
    • 感谢您提供的信息。很高兴您问我问题的目的并试图解决它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2014-06-15
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 2010-10-23
    相关资源
    最近更新 更多