0

嗨,我正在使用 javascript 函数在单击时阻止 ma 文本框,最初文本框包含它们各自的标签名称,例如:客户名称、公司等。单击文本框时,它会使文本框为空,因此可以输入数据。为此,我使用的是 javascript 函数,对于每个文本框,我必须有一个单独的函数,谁能告诉我如何组合所有这些函数,每个函数中唯一不同的是文本框中使用的值和文本框名称.

Javascript函数

    function clientnameclear() {
    if(document.bunkerfrm.clientname.value=="Client Name") {
        var bunkerfrm = document.bunkerfrm.clientname.value="";
        var bunkerfrm = document.bunkerfrm.clientname.focus();
    }
    else {
        var bunkerfrm = document.bunkerfrm.clientname.focus();
    }
}

function clientnamereset() {
    if(document.bunkerfrm.clientname.value=="") {
        var bunkerfrm = document.bunkerfrm.clientname.value="Client Name";
    }
}

function vesselnameclear() {
    if(document.bunkerfrm.vesselname.value=="Vessel Name") {
        var bunkerfrm = document.bunkerfrm.vesselname.value="";
        var bunkerfrm = document.bunkerfrm.vesselname.focus();
    }
    else {
        var bunkerfrm = document.bunkerfrm.vesselname.focus();
    }
}

function vesselnamereset() {
    if(document.bunkerfrm.vesselname.value=="") {
        var bunkerfrm = document.bunkerfrm.vesselname.value="Vessel Name";
    }
}

function compclear() {
    if(document.bunkerfrm.company.value=="Company") {
        var bunkerfrm = document.bunkerfrm.company.value="";
        var bunkerfrm = document.bunkerfrm.company.focus();
    }
    else {
        var bunkerfrm = document.bunkerfrm.company.focus();
    }
}

function compreset() {
    if(document.bunkerfrm.company.value=="") {
        var bunkerfrm = document.bunkerfrm.company.value="Company";
    }
}

Html 代码是

<form name="bunkerfrm" id="bunkerfrm" action="#" method="post"><br>
<input type="text" name="clientname" class="txtbox" value="Client Name" onmousedown="clientnameclear()" onclick="clientnameclear()" onblur="clientnamereset()" />
<br /><br>
<input type="text" name="company" class="txtbox" value="Company" onmousedown="compclear()" onclick="compclear()" onblur="compreset()" />
<br /><br>
<input type="submit" name="submitting" class="bunksubmit" value="Send Your Inquiry" /><br>
 </form>
4

3 回答 3

2

首先,将默认值存储在某处,例如alt给定输入的。

<form name="bunkerfrm" id="bunkerfrm" action="#" method="post"><br>
<input type="text" name="clientname" alt="Client Name" class="txtbox" value="Client Name" onfocus="clear_text(this);" onblur="reset_text(this);" />
<br /><br>
<input type="text" name="company" class="txtbox" alt="Company" value="Company" onfocus="clear_text(this);" onblur="reset_text(this);" />
<br /><br>
<input type="submit" name="submitting" class="bunksubmit" value="Send Your Inquiry" /><br>
 </form>

然后将输入元素this作为参数传递给这些 onfocus/onblur 函数:

function clear_text(elem)
{
    if (elem.value == elem.alt)
        elem.value = "";
}

function reset_text(elem)
{
    if (elem.value == "")
        elem.value = elem.alt;
}

alt如果它的值与存储在属性中的占位符相同,它将在获得焦点时清除输入。该事件onblur将触发该reset_text函数将检查该值是否为空并将其恢复为存储在alt属性中的默认占位符。

于 2012-05-04T02:04:34.307 回答
1

使用placeholder

<input type="text" name="clientname" placeholder="Client Name" class="txtbox" />
<br /><br>
<input type="text" name="company" class="txtbox" placeholder="Company" />
<br /><br>
<input type="submit" name="submitting" class="bunksubmit" placeholder="Send Your Inquiry" /><br>
</form>
于 2012-05-04T02:10:38.833 回答
0

我建议您使用和/或研究现有的库,例如:

于 2012-05-04T02:01:46.680 回答