Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有字符串JavaScript的If-else语句_Javascript_Html_String - Fatal编程技术网

带有字符串JavaScript的If-else语句

带有字符串JavaScript的If-else语句,javascript,html,string,Javascript,Html,String,我正在设计一个页面,当用户做出选择时可以显示分数 但是,此代码似乎不起作用,并且在“分数”字段中不显示任何结果。 我的Html代码: <tr> <td> <div> <input list="typeList" name="type" id="type"> <datalist id="typeList"> <option value="basketball">

我正在设计一个页面,当用户做出选择时可以显示分数

但是,此代码似乎不起作用,并且在“分数”字段中不显示任何结果。 我的Html代码:

<tr>
  <td>
    <div>
      <input list="typeList" name="type" id="type">
      <datalist id="typeList">
        <option value="basketball">
        <option value="soccer">
        <option value="table tenis">
      </datalist>
    </div>
  </td>

  <td><input type="text" id="score" name="score" autocomplete="off"></td>
</tr>
这里应该是
var type=document.getElementById(“type”).value

而且

if (type === 0) {


我在这里使用了Jquery,您犯的错误是,没有对事件进行监视

$('#type')。关于('input',函数(e){
var type=document.getElementById(“type”).value;
var分数=document.getElementById(“分数”);
如果(type==0){score.value=0;}
如果(type=='basketball'){score.value=1;}
如果(type=='soccer'){score.value=2;}
else如果(type=='table tenis'){score.value=3;}
});

您可以访问该元素。您只需要使用元素的
属性。您需要添加带有下拉列表的事件
change
,以便在索引更改时可以绑定
score
文本框中的数据

var type=document.getElementById(“类型”);
var分数=document.getElementById(“分数”);
type.addEventListener('change',function(){
如果(type.value==0){
分值=0;
}else if(type.value==='basketball'){
分值=1;
}else if(type.value==='soccer'){
分值=2;
}else if(type.value==‘乒乓球’){
分值=3分;
}
});

您需要添加一个事件侦听器(或两个,一个用于更改,一个用于输入),然后将代码包装到函数中:

var type=document.getElementById(“类型”);
var分数=document.getElementById(“分数”);
type.addEventListener(“更改”,函数(){
myFunction(type.value);
});
type.addEventListener(“输入”,函数(){
myFunction(type.value);
});
var myFunction=函数(类型){
如果(类型==0){
分值=0;
}如果为else(类型=='basketball'){
分值=1;
}else if(类型==='soccer'){
分值=2;
}else if(类型==='table tenis'){
分值=3分;
}
}

在您的

type.value

获取输入框的值。什么时候运行脚本?

这里是纯JavaScript和JSFIDLE

// you forgot to attach the value property of the type element
var typeValue = document.getElementById("type").value; 
// the result for typeValue is allways a string or undefined

var scoreElement = document.getElementById("score");

// a swtich case is more efficient here
swtich(typeValue){
  case 'basketball':
    scoreElement.value = 1;
    break;
  case 'soccer':
    scoreElement.value = 2:
    break;
  case 'table tenis'
    scoreElement.value = 3;
    break;
  default:
    sourceElement.value = 0;
}

您是否尝试过在调试器中逐行遍历代码?
类型的值是多少?
type
的类型是什么?
document.getElementById()
(与许多其他DOM函数一样)返回一个DOM
元素。不确定为什么需要整数或字符串。是否有任何EventHandler?因为脚本会立即执行并且只执行一次,而且两个输入字段都是空的。例如,您必须将脚本包含在函数中,并将eventhandler附加到按钮。您的脚本在页面加载时运行,因此,我猜没有输入任何内容-javascript不“只知道”您想在每次更改
类型时运行此代码
-您必须告诉它这样做,所以在选择类型后何时调用
javascript
?请参见
“0”==0
为false。
if (type === "0") {
type.value
// you forgot to attach the value property of the type element
var typeValue = document.getElementById("type").value; 
// the result for typeValue is allways a string or undefined

var scoreElement = document.getElementById("score");

// a swtich case is more efficient here
swtich(typeValue){
  case 'basketball':
    scoreElement.value = 1;
    break;
  case 'soccer':
    scoreElement.value = 2:
    break;
  case 'table tenis'
    scoreElement.value = 3;
    break;
  default:
    sourceElement.value = 0;
}
document.getElementById('type').addEventListener('input', function () {
    var type = document.getElementById("type").value;
    var score = document.getElementById("score");

    if (type === '0') {
        score.value = 0;
    } else if (type === 'basketball') {
      score.value = 1;
    } else if (type === 'soccer') {
      score.value = 2;
    } else if (type === 'table tenis') {
      score.value = 3;
    }
 });