隐藏地址字段,直到文本输入- PostcodeAnywhere PHP


if statement - Hide address fields until text entered - PostcodeAnywhere PHP

我试图使用PostCodeAnywhere设置地址表单。搜索工作正常,并按预期自动填充字段。

但是,我想找到一种方法,如何在页面上只显示邮政编码字段,然后当地址输入Address1, Address2, Town, County和Country字段时…这些字段将显示在页面上。

是否有一个简单的方法来做这个使用IF语句?ie。PHP可以实时检查输入字段是否为空吗?

到目前为止,我的表单看起来是这样的:
<li>
    <label>Address 1</label>
    <input type="text" name="address1" value="<?php if (isset($_POST['address1'])) {echo $_POST['address1'];}?>" />(*)
</li>
<li>
    <label>Address 2</label>
    <input type="text" name="address2" value="<?php if (isset($_POST['address2'])) {echo $_POST['address2'];}?>" />
</li>
<li>
    <label>Town</label>
    <input type="text" name="town" value="<?php if (isset($_POST['town'])) {echo $_POST['town'];}?>" />
</li>
<li>
    <label>County</label>
    <input type="text" name="county" value="<?php if (isset($_POST['county'])) {echo $_POST['county'];}?>" />
</li>
<li>
    <label>Postcode</label>
    <input type="text" name="postcode" class="homeReadMo" value="<?php if (isset($_POST['postcode'])) {echo $_POST['postcode'];}?>" />
</li>

isset值,用于从记录中提取已设置的详细信息。很明显!

编辑

我使用jQuery与WordPress在这个网站上,并有它的工作与其他元素。

下面的链接是我放在头部的(我现在不能访问它,所以不能粘贴它)。但我不确定这些细节有什么关系。这需要是一个类的一个包含div围绕预期的隐藏字段吗?还是每个要隐藏的字段都需要有这个类?

编辑

那么在head标签中也就是其他jQuery内容所在的地方我有:

$(".details").each(function (i) {
if ($('input[name$=postcode][value=""]',this).length == 1) { 
    $(this).hide();
    console.log('hiding');
} else {
    $(this).show();
    console.log('showing');
}
});

现在我的表单看起来像这样:

<div class="details">
    <li>
        <label>Address 1</label>
        <input type="text" name="address1" value="<?php if (isset($_POST['address1'])) {echo $_POST['address1'];}?>" />(*)
    </li>
    <li>
        <label>Address 2</label>
        <input type="text" name="address2" value="<?php if (isset($_POST['address2'])) {echo $_POST['address2'];}?>" />
    </li>
    <li>
        <label>Town</label>
        <input type="text" name="town" value="<?php if (isset($_POST['town'])) {echo $_POST['town'];}?>" />
    </li>
    <li>
        <label>County</label>
        <input type="text" name="county" value="<?php if (isset($_POST['county'])) {echo $_POST['county'];}?>" />
    </li>
</div>
<li>
    <label>Postcode</label>
    <input type="text" name="postcode" class="homeReadMo" value="<?php if (isset($_POST['postcode'])) {echo $_POST['postcode'];}?>" />
</li>

编辑

在没有任何运气之后,我尝试了使用这个jQuery代码的不同方法:

if($('#address1').val() == "") {
        $(.details).hide();
    } else {
        $(.details).show();
    }

表单字段正在被隐藏…但是,即使地址1字段有预先由PostcodeAnywhere填充的文本,它们仍然是隐藏的。什么好主意吗?

解决

我设法解决它自己使用以下jQuery代码:

$('#postcode').on('change', function (event) {
if (inputHasContent($(this))) {
    $('.details').show();
} else {
    $('.details').hide();
}
});

我能够通过使用这个JS代码解决我的问题

$('#postcode').on('change', function (event) {
if (inputHasContent($(this))) {
    $('.details').show();
} else {
    $('.details').hide();
}
});