根据用户html-select输出字符串

时间:2019-04-19 13:26:11

标签: javascript html-select

我有一个表单,其中用户输入1到10之间的数量,还有一个只读文本字段,我需要输出带有计算出的价格的字符串。也可以成为容器的另一个王者,但是由于该位置位于格式化的单元格内,所以我不希望使用div。如前所述,表单中的任何内容都可以将值(也有验证码)发布到php文件中以进行购买。就我而言,这是我的代码:

function calculatePrice() {
  var form = document.getElementById('buy_form');
  var num_pieces = parseInt(form.elements.number_select.value);
  var price_for_1 = 9.90; //I know, hardcoded is worst, I'll take it from the DB later on
  var totalprice = price_for_1 * num_pieces;
  document.getElementById("total_price").innerHTML = "Total price: \u20AC " + totalprice;
}
<form method="post" id="buy_form" action="buy.php">

<select name="numberselect" id="number_select" onChange="calculatePrice()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>

<input type=text class="totalprice" id="total_price" style="border: none" readonly>

//a captcha image
//a captcha input field
//a submit button

</form>

该函数根本不会在只读文本字段中输出任何内容。 我想念什么?

4 个答案:

答案 0 :(得分:1)

document.getElementById("total_price").innerHTML 

应该是

document.getElementById("total_price").value

function calculatePrice() {
        var form = document.getElementById('buy_form');
        var num_pieces = parseInt(form.elements.number_select.value);
        var price_for_1 = 9.90; //I know, hardcoded is worst, I'll take it from the DB later on
        var totalprice = price_for_1 * num_pieces;
        document.getElementById("total_price").value = "Total price: \u20AC " + totalprice;
    }
<form method="post" id="buy_form" action="buy.php">

    <select name="numberselect" id="number_select" onChange="calculatePrice()">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
    </select>

    <input type=text class="totalprice" id="total_price" style="border: none" readonly>
</form>

答案 1 :(得分:1)

实际上是

 document.getElementById("total_price").innerHTML

innerHTML将永远无法工作,因为innerHTML会将HTML元素添加到用javascript获取的元素上

应使用

document.getElementById("total_price").value

所以功能看起来像这样

function calculatePrice() { 
  var form = document.getElementById('buy_form');
  var num_pieces = parseInt(form.elements.number_select.value);
  var price_for_1 = 9.90; //I know, hardcoded is worst, I'll take it from the DB later on
  var totalprice = price_for_1 * num_pieces;
  document.getElementById("total_price").value = "Total price: \u20AC " + totalprice;
}

希望这会有所帮助

答案 2 :(得分:0)

在您的JavaScript代码中进行以下更改:

function calculatePrice() {
  var form = document.getElementById('buy_form');
  var num_pieces = parseInt(form.elements.number_select.value);
  var price_for_1 = 9.90; //I know, hardcoded is worst, I'll take it from the DB later on
  var totalprice = price_for_1 * num_pieces;
  document.getElementById("total_price").value = "Total price: \u20AC " + totalprice;
}

window.calculatePrice = calculatePrice;
  1. 将innerHTML更改为值。
  2. 将功能添加到窗口对象。

答案 3 :(得分:0)

这可能是您缺少的这个小东西。如果这是本地HTML和JS,则事件为 onchange ,而不是 onChange

尝试一下!