JavaScript IF语句无法识别值

时间:2017-10-27 01:58:31

标签: javascript html if-statement



function init(){
	document.getElementById("add_soldier_button").onclick = addSoldier;
	document.getElementById("deploy_squad_button").onclick = deploySquad;
}
function Soldier(allegiance, armored, weapons){
	this.allegiance = allegiance;
	this.weapons = weapons;
	this.armored = armored;
	
}
function checkSoldierConstraints(allegiance, armored, weapons){
	
	if(weapons.length == 0)
		return "No weapons specified";
	
	if(armored == "")
		return "Armored not set";
	
	if(allegiance == "")
		return "No allegiance specified";
	
	if (allegiance.value != "khorne" || "slaanesh" || "nurgle" || "tzeentch")
		return "allegiance must be to khorne, slaanesh, nurgle, or tzeentch";
	
	if (weapons.value  != "chainsword" || "plasma cannon" || "bolt rifle" || "magic")
		return "Weapon must be a chainsword, plasma cannon, bolt rifle , or magic";
		
	if ( allegiance.value == "tzeentch" && weapons.value != "magic") 
                return " tzeentch can only use magic";		
	
	soldiers.push(new Soldier(allegiance, armored, weapons));
	return "Creating Soldier";
	
	
	
	
	}
	
	var soldiers = new Array();
function addSoldier(){
	
	
	var allegiance_text = document.getElementById("allegiance_input");
	var armored_text = document.getElementById("armored_input");
	var weapon1_text = document.getElementById("weapon1_input");
	var weapon2_text = document.getElementById("weapon2_input");
	
	var weapons = new Array();
	if(weapon1_text.value != "")
		weapons.push(weapon1_text.value);
	if(weapon2_text.value != "")
		weapons.push(weapon2_text.value);
	
	alert(checkSoldierConstraints(allegiance_text.value, armored_text.value, weapons));
}

function deploySquad(){
	var squad_table = document.getElementById("squad_table");
	while(soldiers.length){
	soldier = soldiers.pop();
		var td1 = document.createElement("td");
		var td2 = document.createElement("td");
		var td3 = document.createElement("td");
	
		td1.innerHTML = soldier.allegiance;
		td2.innerHTML = soldier.armored;
		
	
		if(soldier.weapons.length > 1)
			td3.innerHTML = soldier.weapons[0] + ", " + soldier.weapons[1];
		else
			td3.innerHTML = soldier.weapons[0];
		var row = document.createElement("tr");
		row.appendChild(td1);
		row.appendChild(td2);
		row.appendChild(td3);
		squad_table.appendChild(row);
	} 
} 
window.onload = init;

<!doctype HTML>
<html>
<head>

<title> Brandon Bell </title>

<script src = "objects_part2.js"> </script>

<style>
    table, th, td {
      border: 1px solid black;
      border-collapse: collapse;
      text-align: center;
    }	
</style>

</head>

<body>
<h1>Squad Builder</h1>
    <input type="text" id="allegiance_input" placeholder=" Allegiance">
    <input type="text" id="armored_input" placeholder="Armored (True/False)"><br>
    <input type="text" id="weapon1_input" placeholder="Weapon 1">
    <input type="text" id="weapon2_input" placeholder="Weapon 2"><br><br>
    <input type="button" id="add_soldier_button" value="Add Soldier">
    <input type="button" id="deploy_squad_button" value="Deploy Squad">
</br></br>
<table id="squad_table" width=50%>
  <tr>
    <th>Allegiance</th>
    <th>Armored</th>
    <th>Weapons</th>
   </tr>
</table>
</body>
</html>
&#13;
&#13;
&#13;

我试图让一些if语句起作用。特别是如果你是tzeenech你必须只使用魔法的那个。但即使我提出了tzeenech的忠诚,它也说我必须选择我的代码中解释的那些。但当我使用我所说的我可以使用它不会让我。我也不明白是否因为我做了.value与否。

0 个答案:

没有答案