如何使用JavaScript将所选选项的值与选项文本相同

时间:2019-02-06 15:58:09

标签: javascript html forms dom html-select

我有这种动态的HTML / JavaScript格式,效果很好,但是我发现单击“提交”时,它将选项的值作为索引而不是选项中的文本。

我曾尝试更改JavaScript代码,但似乎无法找出解决方法。

HTML

$variable = [
"Name: " . $name . "<br/>" .
"Product: " . $product . "<br/>
];

$event->description = $variable;
$event->save();

JavaScript

    <!-- 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>

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

    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('decision', decision);

理想情况下,我想看到这是“子类别”的控制台:

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>