我正在进行html
纸牌游戏,并且它的if
语句大于或等于比较运算符。但是,如果资金低于它仍然运行的价格,就好像你有所需的资金一样。
HTML
<article id="moneysys">
<p id="ptdabtd" >Click 'Set Cash' To Enter A Starting Ammount. (Button Will Be Deleted Once Finished)</p>
<button id="scda" onclick="setcash();">Set Cash.</button>
<br />
<div class="cash">Cash: <span id="cash">0</span></div>
<div>Pack Price: <span id="packprice">50</span></div>
</article>
<main>
<center>
<button class="open" onclick="opencards()">Open Cards.</button>
<br />
<img class="card" src="images/blank.png" id="canvas" />
<img class="card" src="images/blank.png" id="canvas1" />
<img class="card" src="images/blank.png" id="canvas2" />
<img class="card" src="images/blank.png" id="canvas3" />
<img class="card" src="images/blank.png" id="canvas4" />
</center>
</main>
JAVASCRIPT
function setcash(){
var wcash = prompt("Please Enter How Much Cash You Want To Start With", "Cash Wanted");
document.getElementById("cash").innerHTML = wcash;
document.getElementById("scda").remove();
document.getElementById("ptdabtd").remove();
}
var cards = new Array("images/1.png", "images/2.png", "images/3.png", "images/4.png", "images/5.png","images/6.png", "images/7.png", "images/8.png", "images/9.png");
var pack = document.getElementById("packprice");
var ccash = document.getElementById("cash");
var pc = Number(pack);
var cash = Number(ccash);
function opencards(){
if (cash >= pc){
var randomNum = Math.floor(Math.random() * cards.length);
document.getElementById("canvas").src = cards[randomNum];
document.getElementById("canvas1").src = cards[randomNum];
document.getElementById("canvas2").src = cards[randomNum];
document.getElementById("canvas3").src = cards[randomNum];
document.getElementById("canvas4").src = cards[randomNum];
cash = cash - pc;
} else{
alert("not")
}
};
答案 0 :(得分:3)
你只获得元素本身,而不是那些元素的实际价值。然后,您尝试从元素中获取数字。由于实际元素不会改变(无论innerhtml设置为什么),因此每次比较时都会得到相同的结果。
var pack = document.getElementById("packprice"); //this gets you the element
var ccash = document.getElementById("cash"); //this gets you the element
尝试改为:
var pack = document.getElementById("packprice").innerHTML;
var ccash = document.getElementById("cash").innerHTML;
你也可以省略
var pc = Number(pack);
var cash = Number(ccash);
答案 1 :(得分:3)
您没有获得ccash
或var pack = document.getElementById("packprice");
var ccash = document.getElementById("cash");
变量的值。因为您没有使用 innerHTML 。
innerHTML属性设置或返回元素的HTML内容(内部HTML)。
var pack = document.getElementById("packprice").innerHTML;
var ccash = document.getElementById("cash").innerHTML;
用此
替换您的代码{{1}}
答案 2 :(得分:0)
你能做点什么:
The parameterized query '(@value nvarchar(4000))INSERT INTO Monday (sixth) values (@value' expects the parameter '@value', which was not supplied.
请在if语句之前。