清除文本框
希望得到任何人的帮助......下面的PHP包含一个来自txt文件的项目列表,每个项目旁边都有一个用于输入数量的文本框,默认情况下设置为1 。我试图在用户点击文本框时清除该框,有什么想法?清除文本框
<input type='text' id='qty' name='$partno' size='1' value='0'>
到目前为止,我尝试过使用JavaScript,但没有运气。
<script>
function validateName()
{
var x=document.forms["purchase"]["visitor"].value;
if (x==null || x=="")
{
alert("Visitor name must be entered");
return false;
}
}
function name()
{
document.getElementById('qty').value = "";
}
</script>
</head>
<body>
<h1>Items Available</h1>
<form id="purchase" name="purchase" action="confirm.php" method="post" onsubmit="return validateName()">
<table>
<h3>Visitor Name: <input type='text' name='visitor'></h3>
<tr><th></th><th></th><th></th><th>Price</th><th>QTY</th></tr>
<?php
if (!($data = file('items.txt'))) {
echo 'ERROR: Failed to open file! </body></html>';
exit;
}
foreach ($data as $thedata) {
list($partno, $name, $description, $price, $image) = explode('|', $thedata);
echo "<tr><td><img src='$image' width='60' height='60' alt='image'></td><td><h3>$name</h3></td><td> $description</td>
<td> <b>£$price</b></td><td><input type='text' id='qty' name='$partno' size='1' value='0'></td></tr>";
//<input type='text' size='1' name='partno_qty' value='1'</td>
}
?>
</table>
<input type="submit" value="Purchase">
<input type="reset" value="Clear">
</form>
添加一个onclick或聚焦状态的处理程序到你的“输入名称=数量”的标签,或者看看使用“占位符”属性,这可能不是在某些浏览器中工作,你必须支持:
foreach ($data as $thedata)
{
list($partno, $name, $description, $price, $image) = explode('|', $thedata);
echo '<tr><td><img src="' . $image . '" width="60" height="60" alt="image"></td>'
. '<td><h3>' . $name . '</h3></td><td> ' . $description . '</td>'
. '<td> <b>£' . $price . '</b></td>'
. '<td><input type="text" id="part-' . $partno . '" '
. 'name="' . $partno . '" size="1" '
. 'value="0" onfocus="this.value=\'\';" placeholder="0"></td>'
. '</tr>';
}
“onclick/onfocus”方法会在每次点击/输入时清除方框。如果你想用你的 “名” 功能,调用更改为:
onfocus="name(this);"
与 “名” 的变化:
function name(obj)
{
obj.value = "";
}
可能会更好地使用onFocus,以便它可以工作,如果用户选项卡 – jszobody 2013-03-27 19:41:54
我不能,因为名称是一个变量。 – user2170008 2013-03-27 19:43:15
当然可以。如果您必须调用一个函数来执行该操作,则可以_always_将“this”传递给它,这是对接收事件的对象的引用。 – 2013-03-27 19:46:32
你可以使用HTML5 placeholder
和required
属性:
<form>
<input type="text" placeholder="Name" required />
<input type="submit" value="submit" />
</form>
说明:
Internet Explorer 10,Firefox,Opera,Chrome和Safari支持placeholder
属性。 Internet Explorer 10,Firefox,Opera和Chrome支持required
标记。这不是在Safari
支持,或者你可以做到这一点的JS是这样的:
<input type="text" onfocus="inputFocus(this, 'initial text')" onblur="inputBlur(this, 'initial text')" value="initial text" />
'initial text'
是文本输入的默认值时,页面加载e.g(“你的名字”)。
function inputFocus(elm, placeholder){
if (elm.value == placeholder){ elm.value = "" }
}
function inputBlur(elm, placeholder){
if (elm.value == "" || elm.value == placeholder){
elm.style.border = "1px solid red";
elm.value = placeholder;
alert("Visitor name must be entered");
}else{
elm.style.border = "1px solid green";
}
}
的PHP因为这是相当无关的问题;我认为如果您为了更好的可读性而发布不带PHP的HTML,会很有帮助。 – MichD 2013-03-27 19:39:43
另外,您在for循环中设置了相同的id属性,导致在同一页上出现重复的id。一个ID只能在HTML文档中出现一次;它需要是独一无二的。 – MichD 2013-03-27 19:40:31