0

我有一个基本的客户信息表,我想让它更具交互性。这个想法是,如果用户选择的国家不是美国或加拿大,则州选择会转到州文本框。我确信这可以只用 php 来完成,但已经使用 jquery 进行了辩论,但如果可能的话想避免它。这是我的表单示例:

<tr>
    <td>
        <input name="city" type="text" id="city" value="<?= $this->aBillingProf['cCity']; ?>" />
    </td>
    <td>
        <select name="state" id="state" style="width:218px;position:relative;top:-4px;">
            <? $s = strlen($this->aBillingProf['cState']) == 2 ? strtoupper($this->aBillingProf['cState']) : strtoupper(stateTranslate($this->aBillingProf['cState']));
            echo stateSelect('both',$s); ?>
        </select>
        <input name="state" type="text" id="state" value="<?= $this->aBillingProf['cState']; ?>" />
/*The Idea being only one of the above options is available depending on what is selected from the country select */

    </td>
</tr>
<tr>
    <td>
        <label for="zip">Zip / Postal Code</label>
    </td>
    <td>
        <label for="country">Country</label>
    </td>
</tr>
<tr>
    <td>
        <input name="zip" type="text" id="zip" maxlength="6" value="<?= $this->aBillingProf['cZip']; ?>" />
    </td>
    <td>
        <select name="country" id="country" style="width:218px;position:relative;top:-4px;">
            <?= AffiliateStore::countrySelect($this->aBillingProf['cCountry']); ?>
        </select>
        <!-- <input type="text" name="country" id="country" value="<?= $this->aBillingProf['cCountry']; ?>" /> -->
    </td>
</tr>

感谢您的任何意见或建议

4

2 回答 2

1

基本上我所做的是添加一个包含我需要的输入的 div。在这种情况下,我会为美国和加拿大各州使用选择框,为其他州使用文本输入。使用 CSS diplay:none 折叠所有 div,除了默认选定国家/地区的 div。

<div id="state_inputs">
    <div id="us_states_div">
        <!-- Select box for US states goes here -->
    </div>
    <div style="display:none" id="ca_states_div">
        <!-- Select box for US states goes here -->
    </div>
    <div style="display:none" id="other_states_div">
        <input type="text" name="other_states" />
    </div> 
</div>

现在假设您的国家选择框具有 id country_select:

<script>
document.getElementById("country_select").onchange = function() {
        var country_select = document.getElementById("country_select");
        var country = country_select.options[country_select.selectedIndex].text;

        if(country == "USA") {
            document.getElementById("us_states_div").style.display="block";
            document.getElementById("ca_states_div").style.display="none";
            document.getElementById("other_states_div").style.display="none";
        } else if(country == "Canada") {
            document.getElementById("us_states_div").style.display="none";
            document.getElementById("ca_states_div").style.display="block";
            document.getElementById("other_states_div").style.display="none";
        } else {
            document.getElementById("us_states_div").style.display="none";
            document.getElementById("ca_states_div").style.display="none";
            document.getElementById("other_states_div").style.display="block";
        }
    };
</script>

一旦您更新了 PHP 以输出该标记,在处理端您只需查看所选国家/地区,然后使用正确状态输入中包含的值。

于 2013-06-19T21:55:55.900 回答
0

仅使用 PHP 无法做到这一点,因为它要求您使用客户端脚本来确定事件何时被触发。您可以使用 PHP 来重建表单,但是,这需要从服务器重新加载。使用 JavaScript 处理事件和更新表单要容易得多。

于 2013-06-19T21:33:31.557 回答