javascript - 在 <p> 标签中创建文本取决于 <select> 标签,javascript

标签 javascript html select input

我想在输入标签后创建带有文本(或类似内容)的 p 标签,在用户在选择标签中选择国家/地区之前,通知用户他/她在此字段中输入的值。
例如:
用户选择USA:输入后必须出现“USD”
用户选择英格兰:输入后必须出现“GBP”

请解释一下为什么我的代码不起作用,这是正确的方法吗?

JS:

function insertAfter(referenceNode, newNode) 
{
     referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

function createText() 
{
     var selectValue = document.getElementById("money").value;
     var CashInput = document.getElementById("cash");
     var temp = document.createElement("p");
     if (selectValue == "pound") 
     {
         var text = "GBP";
         temp.appendChild(text);
         insertAfter(CashInput, temp);
     }
}

HTML:

<select id="money" onchange="createText();">
    <option value="pound">England</option>
    <option value="doll">USA</option>
</select>

    <input id="cash">

最佳答案

我建议使用现有元素并仅更改其 insideText 属性。这是一个功能示例。

function insertAfter(referenceNode, newNode) 
{
     referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

function createText() 
{
     var selectValue = document.getElementById("money").value;
     var CashInput = document.getElementById("cash");
     if (selectValue == "pound") 
     {
         var text = "GBP";
     } else if (selectValue == "doll") {
         text = "USD";
     }
     document.getElementById("currency").innerText = text;
}
createText();
<select id="money" onchange="createText();">
<option value="pound">England</option>
<option value="doll">USA</option>
</select>


<input id="cash">
<span id="currency"></span>

关于javascript - 在 <p> 标签中创建文本取决于 <select> 标签,javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752371/

相关文章:

c++ - 使用单个文件描述符,select,poll和epoll和...之间是否有任何性能差异?

javascript - 如何设置前端编码环境?

javascript - 意想不到的颜色?

javascript - 从 Quickbase 中的 html 文件引用 js 文件

javascript - 无法在首页加载时使用 HTML5 和 Google Chrome 中的 getUserMedia() 从网络摄像头拍照

javascript - JSON解析成HTML,对象如何映射到对应的HTML?

html - 定义宽度和高度后,flex容器中的图像未居中

mysql - 选回不存在的东西

javascript - 在 Visual Studio 2010 中使用从 mindscape 网络工作台(coffeescript 源)生成的 javascript

SQL 通过多个列从表中选择不同的行,忽略列顺序(意义)