0

我有一个用于估算运费的表单字段。目前该字段有一个标签“邮政编码”,并且表单字段本身默认为空。我想做以下事情:

  1. 将标签“放在”表单字段中,使其显示“输入您的邮政编码...”(可能是 value="

  2. 一旦用户将光标放到表单域中,默认文本就会被删除。

  3. 一旦用户输入他的邮政编码并单击提交按钮,他的邮政编码(=他输入的值)就会在表单字段中回显,从而替换默认文本

这是我正在使用的代码:

<label for="postcode" <?php echo $this->__('Zip/Postal Code') ?></label>
<input class="input-text validate-postcode<?php if ($this->isZipCodeRequired()):?>
 required-entry<?php endif;?>" type="text" id="postcode" name="estimate_postcode" 
 value="<?php echo $this->htmlEscape($this->getEstimatePostcode()) ?>" />

我怎样才能做到这一点?

4

2 回答 2

3

您可以使用 HTML5 占位符属性使文本在客户端消失。要使其跨浏览器,请使用jQuery Placeholder 插件(或类似插件)。

根据您提交表单的方式,您将拥有 *estimate_postcode* 作为 GET 或 POST 变量。

$_GET['estimate_postcode']

$_POST['estimate_postcode']

将其作为值属性回显。但是,这不会在页面之间持续存在。

如果您这样做是为了用户的视觉利益,请放弃 PHP 并通过 cookie 在客户端完成所有操作。

于 2012-05-24T22:17:52.703 回答
0
<script>
function clearField()
{
  $(#postal_code).val('');
}
</script>


 <form>
 <input id = "postal_code" type = "text" name = "postal_code" value = "Input postal code here" onclick="clearField()"/>
 </form>

像这样的东西应该工作。当用户单击它时,clearField 只会清空该字段,然后输入的数据将通过表单发送。

于 2012-05-24T22:16:38.973 回答