0

为班级工作二十一点游戏,显示功能出现问题。它确实改变了字段的值,但在函数完成后它又恢复了。如何使更改持久?

<html>

<head>
<script type="text/javascript">
function stack()
{
this.cards = new Array();

this.makeDeck = buildDeck;
this.deal = dealCard;
this.add = addCard;
}

function dealCard()
{
return this.cards.shift();
}

function addCard(card)
{
    this.cards.push(card);
}


function buildDeck()
{
var ranks = new Array("Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King");
    var suits = new Array("Clubs", "Diamonds", "Hearts", "Spades");

    this.cards = new Array(52);

    for (s = 0; s < suits.length; s++)
    {
      for (r = 0; r < ranks.length; r++)
      {
         this.cards[(s * ranks.length) + r] = new card(ranks[r], suits[s]);
      }
    }
}

function card(r, s)
{
this.rank = r;
this.suit = s;
}

function display()
{

document.getElementById("player_card_1").value = "anything";
alert("here");
}


function newGame()
{
var Player = new stack();
var Dealer = new stack();
var Deck = new stack();
Deck.makeDeck();
Player.add(Deck.deal());

display();
}

</script>
<style type="text/css">
input {
    background:transparent
}
</style>

</head>

<body>
<table bgcolor="#33CC33">
    <tr>
        <td style="font:14pt bold">Your cards</td>
        <td></td>
        <td style="font:14pt bold">Dealer's cards</td>
        <td></td>
    </tr>
    <tr>
        <td><input type="text" readonly="readonly" value="" id="player_card_1"></td>
        <td><input type="text" readonly="readonly" value="" id="p_card_1_val"></td>
        <td><input type="password" readonly="readonly" value="" id="dealer_card_1"></td>
        <td><input type="password" readonly="readonly" value="" id="d_card_1_val"></td>
    </tr>
    <tr>
        <td><input type="text" readonly="readonly" value="" id="player_card_2"></td>
        <td><input type="text" readonly="readonly" value="" id="p_card_2_val"></td>
        <td><input type="text" readonly="readonly" value="" id="dealer_card_2"></td>
        <td><input type="text" readonly="readonly" value="" id="d_card_2_val"></td>
    </tr>
    <tr>
        <td><input type="text" readonly="readonly" value="" id="player_card_3"></td>
        <td><input type="text" readonly="readonly" value="" id="p_card_3_val"></td>
        <td><input type="text" readonly="readonly" value="" id="dealer_card_3"></td>
        <td><input type="text" readonly="readonly" value="" id="d_card_3_val"></td>
    </tr>
    <tr>
        <td><input type="text" readonly="readonly" value="" id="player_card_4"></td>
        <td><input type="text" readonly="readonly" value="" id="p_card_4_val"></td>
        <td><input type="text" readonly="readonly" value="" id="dealer_card_4"></td>
        <td><input type="text" readonly="readonly" value="" id="d_card_4_val"></td>
    </tr>
    <tr>
        <td><input type="text" readonly="readonly" value="" id="player_card_5"></td>
        <td><input type="text" readonly="readonly" value="" id="p_card_5_val"></td>
        <td><input type="text" readonly="readonly" value="" id="dealer_card_5"></td>
        <td><input type="text" readonly="readonly" value="" id="d_card_5_val"></td>
    </tr>
    <tr>
        <td><input type="text" readonly="readonly" value="Total"></td>
        <td><input type="text" readonly="readonly" value=""></td>
        <td><input type="text" readonly="readonly" value="Total"></td>
        <td><input type="text" readonly="readonly" value=""></td>
    </tr>
</table>
<br>
<form name="buttons" action="">
<button name="deal" onclick="newGame()">Deal</button>
<button name="hit">Hit</button>
<button name="stand">Stand</button>
</form>

</body>

</html>
4

1 回答 1

0

试试这个代码。将您的表单代码替换为

<form name="buttons" action="">
<input type="button" name="deal" onclick="newGame()"/>Deal
<input type="button" name="hit">Hit</button>
<input type="button" name="stand">Stand</button>
</form>
于 2012-10-29T05:28:26.167 回答