1

我正在尝试将输入框更改为“th”、“st”、“nd”或“rd”,具体取决于在另一个框中输入的数字

我的 HTML 是

<input id="hole_number" name="hole_number" maxlength="3" size="3" onkeydown="stndrdth()" onkeyup="stndrdth()" onkeypress="stndrdth()" onclick="stndrdth()" onfocus="stndrdth()" onchange="stndrdth()" />
<input id="stndrdth" name="stndrdth" size="2" maxlength="2" />

我的javascript是:

function stndrdth()
{
    var theinput = document.getElementById("hole_number").value;
    var righthandnumber = theinput.charAt(theinput.length-1);
    alert('hi');


    if( righthandnumber == 1 )
    {
        document.getElementById("stndrdth").value = '{$lang['st']}';
    }
    else if( righthandnumber == 2 )
    {
        document.getElementById("stndrdth").value = '{$lang['nd']}';
    }
    else if( righthandnumber == 3 )
    {
        document.getElementById("stndrdth").value = '{$lang['rd']}';
    }
    else
    {
        document.getElementById("stndrdth").value = '{$lang['th']}';
    }


}

在我将输入框放在表单标签中之前,它工作得非常好:

<form method="get" ID="hole_form" action="./">
<input id="hole_number" name="hole_number" maxlength="3" size="3" onkeydown="stndrdth()" onkeyup="stndrdth()" onkeypress="stndrdth()" onclick="stndrdth()" onfocus="stndrdth()" onchange="stndrdth()" />
<input id="stndrdth" name="stndrdth" size="2" maxlength="2" />
</form>

然后盒子总是空的

有人可以帮忙吗?

问候李

4

0 回答 0