当客户开始以第一种形式输入时,使用表单显示div

时间:2014-04-04 08:26:04

标签: php forms toggle

我有一份登记表。使用首字母,第二名称和公司名称的文本输入。

如果客户开始输入,则必须下载另一个div,其中包含客户可以提供公司信息的表单。

我有一个旧模块,我试图复制一些代码,但它不会起作用,因为有些功能我不知道。

这是我的代码,有人可以帮助我吗?

抱歉我的英语不好:P

    <div id="NewCustomerDiv" >
    <label>Company name: </label>
    <input type="text" name="CompanyName" value="<?php echo $customer['CompanyName']; ?>"/>
    <br />

    <div id="CustomerCompanyDiv" <?php if(!isset($customer['CompanyName']) || !$customer['CompanyName']){ echo "style=\"display:none;\""; } ?>>
        <label>Company name:</label>
        <input type="text" name="CompanyNumber" value="<?php if(isset($customer['CompanyNumber'])){ echo $customer['CompanyNumber']; } ?>"/>
        <br />

        <label>taxnumber:</label>
        <input type="text" name="TaxNumber" value="<?php if(isset($customer['TaxNumber'])){ echo $customer['TaxNumber']; } ?>"/>
        <br />

        <?php if(count($array_legaltype) > 0){ ?>
        <label>companytax:</label>
        <select name="LegalForm" class="w1">
            <?php foreach($array_legaltype as $k=>$v){ ?>
                <option value="<?php echo $k; ?>" <?php if(isset($customer['LegalForm']) && $customer['LegalForm'] == $k){ echo "selected=\"selected\""; } ?>><?php echo $v; ?></option>
            <?php } ?>
        </select>
        <br />
        <?php } ?>

        <br />
    </div>
    </div>

1 个答案:

答案 0 :(得分:0)

你必须在javascript(客户端)这样做我建议你使用像jquery这样的库它会更容易。

<script type="text/javascript">
//with jQuery
$(document).ready(function(){

   //admiting that the input[name=CompanyName] is the one you want to bind with the div
   $('#NewCustomerDiv input[name=CompanyName]').keyup(function(){
     //admiting that the div you want to show is CustomerCompanyDiv
     if(!$(this).val()){
         $('#CustomerCompanyDiv').hide();
     }
     else{
         $('#CustomerCompanyDiv').show();
     }
   });
   if($('#NewCustomerDiv input[name=CompanyName]').val()){
         $('#CustomerCompanyDiv').show(); 
   }
});
</script> 

<div id="NewCustomerDiv" >
<label>Company name: </label>
<input type="text" name="CompanyName" value="<?php echo $customer['CompanyName']; ?>"/>
<br />

<div id="CustomerCompanyDiv" style="display:none">        <label>Company name:</label>
    <input type="text" name="CompanyNumber" value="<?php if(isset($customer['CompanyNumber'])){ echo $customer['CompanyNumber']; } ?>"/>
    <br />

    <label>taxnumber:</label>
    <input type="text" name="TaxNumber" value="<?php if(isset($customer['TaxNumber'])){ echo $customer['TaxNumber']; } ?>"/>
    <br />

    <?php if(count($array_legaltype) > 0){ ?>
    <label>companytax:</label>
    <select name="LegalForm" class="w1">
        <?php foreach($array_legaltype as $k=>$v){ ?>
            <option value="<?php echo $k; ?>" <?php if(isset($customer['LegalForm']) && $customer['LegalForm'] == $k){ echo "selected=\"selected\""; } ?>><?php echo $v; ?></option>
        <?php } ?>
    </select>
    <br />
    <?php } ?>

    <br />
</div>
</div>

查看实际操作:http://jsfiddle.net/TMG85/