如何操控选定选项的值是一样的JavaScript选项的文本

问题描述 投票:0回答:2

我有一个伟大的工作,这个动态HTML / JavaScript的形式,但我发现,当我点击提交它带来的是在选择,因为它的索引的价值,而不是作为选项中的文本。

我曾试图改变JavaScript代码,但似乎无法弄清楚如何解决这个问题。

HTML

    <!-- Category -->
    <label for="type">Category<span class="required-input"> *</span></label>
    <select id="type" name="type" onchange="ChangeTypeList()">
        <option value="">--Select--</option>
        <option value="Order Inquiry">Order Inquiry</option>
        <option value="Product Inquiry">Product Inquiry</option>
    </select>

    <!-- Sub Category -->
    <label for="reason">Sub Category</label>
    <select id="reason" name="reason"></select>

JavaScript的

    var reasons = {};
    reasons['Order Inquiry'] = ['Order Status','Order Issue', 'X'];
    reasons['Product Inquiry'] = ['Product Weight', 'Product Quality'];

    function ChangeTypeList() {
        var typeList = document.getElementById("type");
        var reasonsList = document.getElementById("reason");
        var reasonsType = typeList.options[typeList.selectedIndex].value;

        while (reasonsList.options.length) {
            reasonsList.remove(0);
        }
        var decisions = reasons[reasonsType];
        if (decisions) {
            var i;
            for (i = 0; i < decisions.length; i++) {
                var decision = new Option(decisions[i], i);
                reasonsList.options.add(decision);
                // console.log('decision', decision);
            }
        }
    }

当我选择的第一个类别选择“订单查询”和的console.log:

console.log('decision', decision);

我看到在控制台的子分类如下:

<option value="0">Order Status</option>
<option value="1">Order Issue</option>
<option value="2">X</option>

理想的情况是我想看到这是为子分类的控制台:

<option value="Order Status">Order Status</option>
<option value="Order Issue">Order Issue</option>
<option value="X">X</option> 
javascript html forms dom html-select
2个回答
0
投票

你需要通过decisions[i]Option第二个参数。 语法:new Option(text, value, defaultSelected, selected);更多细节Option

var reasons = {};
    reasons['Order Inquiry'] = ['Order Status','Order Issue', 'X'];
    reasons['Product Inquiry'] = ['Product Weight', 'Product Quality'];

    function ChangeTypeList() {
        var typeList = document.getElementById("type");
        var reasonsList = document.getElementById("reason");
        var reasonsType = typeList.options[typeList.selectedIndex].value;

        while (reasonsList.options.length) {
            reasonsList.remove(0);
        }
		var reason = document.getElementById('reason');
        var decisions = reasons[reasonsType];
        if (decisions) {
            var i;
            for (i = 0; i < decisions.length; i++) {
				//This line is changed
                var decision = new Option(decisions[i], decisions[i]);
                reasonsList.options.add(decision);
                console.log('decision', decision);
            }
        }
    }
<label for="type">Category<span class="required-input"> *</span></label>
    <select id="type" name="type" onchange="ChangeTypeList()">
        <option value="">--Select--</option>
        <option value="Order Inquiry">Order Inquiry</option>
        <option value="Product Inquiry">Product Inquiry</option>
    </select>

    <!-- Sub Category -->
    <label for="reason">Sub Category</label>
    <select id="reason" name="reason"></select>

0
投票

option HTML元件具有valuetext属性。

var options = document.getElementById("dropdown").children;

for (var i = 0; i < options.length; i++) {
  options[i].value = options[i].text;
}
<select id="dropdown">
  <option value="0">A</option>
  <option value="1">B</option>
  <option value="2">C</option>
</select>
© www.soinside.com 2019 - 2024. All rights reserved.