为什么我的JavaScript函数中的大小写不起作用?

时间:2018-08-02 05:03:32

标签: javascript

当我运行HTML页面并单击选择框时,脚本中的switch-case语句始终返回默认值,而不是特定情况之一。我不知道为什么会这样。这是我的代码:

function getvalue(offer) {

		var code = document.getElementById('channel1');
		var dtc = document.getElementById('channel2');
    var channelX = document.getElementById('channel3');
    var remark = document.getElementById('subchannel');

		switch (offer) {
		case "CC-C":
			code.value = "user1";
			dtc.value = "Position1"+remark;
      channelX.value = "VBnet";
			break;
		case "CC-D":
			code.value = "user2";
			dtc.value = "Postion2";
      channelX.value = "VBnet";
			break;
		default:
			code.value = "Owner";
			dtc.value = "Admin";
      channelX.value = "VBnet";
		}
	}
<select class="validate[required] text-input form-control form-text required" id="channel1" name="submitted[channel1]" onchange="getvalue(this.value)">
          <option value="">Choosechoice
          </option>
          <option value="CC-C">Height
          </option>
          <option value="CC-D">Low
          </option>
</select>          
<select class="validate[required] text-input form-control form-text required" id="subchannel" name="submitted-subchannel" onfocusout="getvalue(this.value)">
<option value="">Choosechoice</option>
 <option value="Walkin">Medium</option>
 <option value="Walkout">Small</option>
 </select>

<input class="form-control form-text" id="channel3" maxlength="128" name="channel3" size="60" style="display:none;" type="text" value="info" />
    <input class="form-control form-text" id="channel2" maxlength="128" name="description" size="60" style="display:none" type="text" value="Choosechoiceplease" /> 

1 个答案:

答案 0 :(得分:0)

好的,这是您的解决方案。你不能说

onchange="getvalue(this.value)"

onfocusout="getvalue(this.value)"

由于HTML无法以这种方式处理对象结构,因此生成的offer始终为undefined

您必须简单地通过这种方式传递

onchange="getvalue(this)"
onfocusout="getvalue(this)"

    <select class="validate[required] text-input form-control form-text required" id="channel1" name="submitted[channel1]" onchange="getvalue(this)">
        <option value="">
            Choosechoice
        </option>
        <option value="CC-C">
            Height
        </option>
        <option value="CC-D">
            Low
        </option>
    </select>
    <select class="validate[required] text-input form-control form-text required" id="subchannel" name="submitted-subchannel" onfocusout="getvalue(this)">
        <option value="">
            Choosechoice
        </option>
        <option value="Walkin">
            Medium
        </option>
        <option value="Walkout">
            Small
        </option>
    </select>
    <input class="form-control form-text" id="channel3" maxlength="128" name="channel3" size="60" style="display:none;" type="text" value="info" /> 
    <input class="form-control form-text" id="channel2" maxlength="128" name="description" size="60" style="display:none" type="text" value="Choosechoiceplease" /> 

然后在脚本中像这样处理它

function getvalue(offer) {

    var code = document.getElementById('channel1');
    var dtc = document.getElementById('channel2');
    var channelX = document.getElementById('channel3');
    var remark = document.getElementById('subchannel');

    console.log('code: ', code.value);
    console.log('dtc: ', dtc.value);
    console.log('channelX: ', channelX.value);
    console.log('remark: ', remark.value);
    console.log('offer: ', offer.value);


    switch (offer.value.trim()) {
        case "CC-C":
            code.value = "user1";
            dtc.value = "Position1"+remark;
            channelX.value = "VBnet";
            console.log('matching CC-C');
            break;
        case "CC-D":
            code.value = "user2";
            dtc.value = "Postion2";
            channelX.value = "VBnet";
            console.log('matching CC-D');
            break;
        default:
            code.value = "Owner";
            dtc.value = "Admin";
            channelX.value = "VBnet";
            console.log('defaulting');
    }
}