javascript - 使用 DOM 创建输入

标签 javascript html dom

我目前一直在研究这段代码,但我似乎无法弄清楚。我计划这样做,以便如果按下单选按钮表示运费不是免费的,则会弹出一个输入字段以指定使用 DOM 的附加成本。我还试图弄清楚如何添加文本来描述输入字段并验证输入字段。

<!DOCTYPE html>
<html>
    <head>        
    </head>
    <body>
        <script>

        function myFunction() {
            var x = document.createElement("INPUT");
            var c = 1;
            if (c = 1) {
            x.setAttribute("type", "text");

            var sp2 = document.getElementById("emailP");            
    //      var br = document.createElement("br");
    //      sp2.appendChild(br);
    //      alert("added break");           
            var sp2 = document.getElementById("emailP");
            var parentDiv = sp2.parentNode;
            parentDiv.insertBefore(x, sp2); 
            c++;
            alert("Added Text Box");
            }
            }
        </script>
        <form action="#" method="post" onsubmit="alert('Your form has been submitted.'); return false;">

            <p class="boldParagraph">Upload an Image:</p>
            <input type="file" id="pic" accept="image/*" required>

            <p class="boldParagraph">Name of seller:</p> 
            <input class="averageTextBox" type="text" id="seller" value="" required>

            <p class="boldParagraph" id = "tip3P">Shipping costs are free:</p>
            <input type="radio" name="tip3" value="3" checked /> Yes
            <input type="radio" name="tip3" value="4" onclick="myFunction(); this.onclick=null;"/> No

            <p class="boldParagraph" id = "emailP">Email of seller:</p>
            <input class="averageTextBox" type="email" id="emailAddress" value="" required>

            <p class="boldParagraph">Closing date for auction:</p> 
            <input type="date" id="closeDate" value="" required>

            <br><br>
            <button>Submit</button>

        </form>
    </body>
</html>

最佳答案

创建一个 label 元素并使用 innerHTML 填充文本。然后附加到 DOM。

示例片段:

function myFunction() {
  var label = document.createElement("label");
  label.innerHTML = "<br>Shipment Cost : ";
  var x = document.createElement("INPUT");
  var c = 1;
  if (c = 1) {
    x.setAttribute("type", "text");

    var sp2 = document.getElementById("emailP");
    //      var br = document.createElement("br");
    //      sp2.appendChild(br);
    //      alert("added break");           
    var sp2 = document.getElementById("emailP");
    var parentDiv = sp2.parentNode;
    parentDiv.insertBefore(x, sp2);
    parentDiv.insertBefore(label, x);
    c++;
    alert("Added Text Box");
  }
}
<form action="#" method="post" onsubmit="alert('Your form has been submitted.'); return false;">

  <p class="boldParagraph">Upload an Image:</p>
  <input type="file" id="pic" accept="image/*" required>

  <p class="boldParagraph">Name of seller:</p>
  <input class="averageTextBox" type="text" id="seller" value="" required>

  <p class="boldParagraph" id="tip3P">Shipping costs are free:</p>
  <input type="radio" name="tip3" value="3" checked />Yes
  <input type="radio" name="tip3" value="4" onclick="myFunction(); this.onclick=null;" />No

  <p class="boldParagraph" id="emailP">Email of seller:</p>
  <input class="averageTextBox" type="email" id="emailAddress" value="" required>

  <p class="boldParagraph">Closing date for auction:</p>
  <input type="date" id="closeDate" value="" required>

  <br>
  <br>
  <button>Submit</button>

</form>

或者

您可以隐藏文本框并在用户单击“否”时显示它。此外,仅当未选择“发货”单选按钮时才应用验证。

我建议使用jQuery ,请参阅下面的代码片段:

jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.

var radioButtons = $("[name=tip3]");
radioButtons.on("change", function() {
  if ($("[name=tip3]:checked").val() == "3") {
    $("#shipmentDetail").hide();
  } else {
    $("#shipmentDetail").show();
  }
})
$("#submit").on("click", function() {
  var flag = true;
  if ($("[name=tip3]:checked").val() == "4") {
    if ($("#shipmentDetail").val() == "") {
      flag = false;
      alert("enter some value");
    }
  }

  //other validations here

  if (flag) {
    $("#form").submit()
  }
})
#shipmentDetail {
  display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form" action="#" method="post">

  <p class="boldParagraph">Upload an Image:</p>
  <input type="file" id="pic" accept="image/*" required>

  <p class="boldParagraph">Name of seller:</p>
  <input class="averageTextBox" type="text" id="seller" value="" required>

  <p class="boldParagraph" id="tip3P">Shipping costs are free:</p>
  <input type="radio" name="tip3" value="3" checked />Yes
  <input type="radio" name="tip3" value="4" />No
  <label id="shipmentDetail" for="price">Shipment Cost:
    <input id="price" type="text" value="" />
  </label>
  <p class="boldParagraph" id="emailP">Email of seller:</p>
  <input class="averageTextBox" type="email" id="emailAddress" value="" required>

  <p class="boldParagraph">Closing date for auction:</p>
  <input type="date" id="closeDate" value="" required>

  <br>
  <br>
  <button id="submit">Submit</button>

</form>

关于javascript - 使用 DOM 创建输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32976301/

相关文章:

javascript - 如何在全日历日 View 中更改默认行高?

javascript - 在ajax中使用post方法

javascript - AJAX GET 调用将在第一次调用时起作用,但在其他点击后不起作用

javascript - bootstrap 的不稳定问题 -> 仅在大屏幕上行宽度不一致

html - 如何垂直对齐图像和文本跨客户端电子邮件模板

html - 使用页面分辨率制作 HTML

java - 如何在 java 中生成一个大的(30MB+)xml 文件?

Javascript 下拉菜单

Javascript - document.createDocumentFragment() - 通过 ID 引用元素

javascript - 如何使用 JavaScript 测量 DOM 节点内容区域的尺寸?