【问题标题】:Hiding particular divs on selecting dropdown values in codeigniter在codeigniter中选择下拉值时隐藏特定的div
【发布时间】:2017-03-21 15:00:40
【问题描述】:

选择下拉值无法隐藏特定的 div。这是我的代码。

查看:

function showRequiredOption(cval)
	{
		if(cval=='interstate')
		{
			$('#cgst').hide();
			$('#sgst').hide();
			
		}
		else if(cval=='exemptedsales')
		{
			$('#cgst').hide();
			$('#sgst').hide();
			$('#igst').hide();
			
		}
		else
		{
			$('#igst').hide();
			$('#cgst').show();
			$('#sgst').show();
			
		}
	}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-9 col-md-offset-2">
	<div id="legend">
		<legend class="">Profile Information</legend>
	</div>  
    <form role= action="<?php echo site_url();?>invoice/addinvoice" method="post" class="form-horizontal" id="location" method="post" accept-charset="utf-8">		
		<div class="form-group">
			<label class="control-label col-sm-2 " for="user">User</label> 
			<div class="col-sm-4 col-sm-offset-1">
				<select id="user" name="user" onchange="showRequiredOption(this.value);">
					<option value="employee">Local Sales</option>
					<option value="interstate">Inter state</option>
					<option value="freelancer">Stock transfers</option>
					<option value="exemptedsales">Exempted Sales</option>
					<option value="company">Zero Rated Sales</option>
					<option value="freelancer">Job Work</option>
				</select>
			</div>
		</div>		
		<div class="form-group">
		  <label class="control-label col-sm-2 " for="hsn">Freight Charges</label>
		  <div class="col-sm-4 col-sm-offset-1">
			<input type="text" class="form-control" id="hsn" placeholder="Enter HSN Number"  >
		  </div>
		</div>
		
		<div class="form-group" id="igst">
		  <label class="control-label col-sm-2 " for="hsn">IGST 18%</label>
		  <div class="col-sm-4 col-sm-offset-1">
			<input type="text" class="form-control" id="hsn" placeholder="Enter HSN Number"  >
		  </div>
		</div>	
		 <div class="form-group" id="sgst">
		  <label class="control-label col-sm-2 " for="hsn">SGST 9%</label>
		  <div class="col-sm-4 col-sm-offset-1">
			<input type="text" class="form-control" id="sgst" placeholder="Enter HSN Number"  >
		  </div>
		</div>	
		<div class="form-group" id="cgst">
		  <label class="control-label col-sm-2 " for="hsn">CGST 9%</label>
		  <div class="col-sm-4 col-sm-offset-1">
			<input type="text" class="form-control" id="cgst" placeholder="Enter HSN Number"  >
		  </div>
		</div>			
      <button id="submit" type="submit" class="btn" name="submit">Submit</button>
     </form>
    </div>

在这里,如果我选择州际公路,下拉 cgst 中的股票转移,应隐藏 sgst 而应显示 igst。

如果我选择免税销售、零评级销售、应隐藏下拉菜单中的工作工作 IGST、CGST、SGST。

默认显示 sgst 和 cgst

【问题讨论】:

  • @AndrewLi:很高兴知道,但在那个 sn-p 中几乎没有 PHP 代码,肯定不足以让 sn-p 崩溃。

标签: javascript php jquery html codeigniter


【解决方案1】:

应用else if之间的空格

条件: 选择interstat or Stock Transferscgst,sgst应该被隐藏,igst应该被显示。

在下拉列表中选择Exempted sales,Zero Rated sales,Job Work IGST,CGST,SGST 应该被隐藏。

function showRequiredOption(cval)
{
	if((cval=='interstate') || (cval == "stocktransfers"))
	{
		$('#cgst').hide();
		$('#sgst').hide();
		$('#igst').show();
	}
	else if ((cval=='exemptedsales') || (cval=="zeroratedsales") ||(cval=="jobwork"))
	{
		$('#cgst').hide();
		$('#sgst').hide();
		$('#igst').hide();
		
	}
	else
	{
		$('#igst').hide();
		$('#cgst').show();
		$('#sgst').show();
		
	}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-9 col-md-offset-2">
<div id="legend">
    <legend class="">Profile Information</legend>
</div>  
<form role="form" action="<?php echo site_url();?>invoice/addinvoice" method="post" class="form-horizontal" id="location" method="post" accept-charset="utf-8">     
    <div class="form-group">
        <label class="control-label col-sm-2 " for="user">User</label> 
        <div class="col-sm-4 col-sm-offset-1">
            <select id="user" name="user" onchange="showRequiredOption(this.value)">
                <option value="employee">Local Sales</option>
                <option value="interstate">Inter state</option>
                <option value="stocktransfers">Stock transfers</option>
                <option value="exemptedsales">Exempted Sales</option>
                <option value="zeroratedcompany">Zero Rated Sales</option>
                <option value="jobwork">Job Work</option>
            </select>
        </div>
    </div>      
    <div class="form-group">
      <label class="control-label col-sm-2 " for="hsn">Freight Charges</label>
      <div class="col-sm-4 col-sm-offset-1">
        <input type="text" class="form-control" id="hsn" placeholder="Enter HSN Number"  >
      </div>
    </div>

    <div class="form-group" id="igst" style="display:none;">
      <label class="control-label col-sm-2 " for="hsn">IGST 18%</label>
      <div class="col-sm-4 col-sm-offset-1">
        <input type="text" class="form-control" id="hsn" placeholder="Enter HSN Number"  >
      </div>
    </div>  
     <div class="form-group" id="sgst">
      <label class="control-label col-sm-2 " for="hsn">SGST 9%</label>
      <div class="col-sm-4 col-sm-offset-1">
        <input type="text" class="form-control" id="sgst" placeholder="Enter HSN Number"  >
      </div>
    </div>  
    <div class="form-group" id="cgst">
      <label class="control-label col-sm-2 " for="hsn">CGST 9%</label>
      <div class="col-sm-4 col-sm-offset-1">
        <input type="text" class="form-control" id="cgst" placeholder="Enter HSN Number"  >
      </div>
    </div>          
  <button id="submit" type="submit" class="btn" name="submit">Submit</button>
 </form>
</div>

【讨论】:

  • cval = "freelancer" ---> cval == "freelancer"
  • 默认出现一个问题,它显示 igst 它不应该显示
  • 默认local sales是哪一个?
  • igst 不应该只显示 cgst 和 sgst 应该显示
  • 查看更新的答案。i 已与 switch 语句一起应用。使用 if 语句会更好
【解决方案2】:

这只是一个关于如何实现这一目标的简单示例。希望你能通过这个找到答案。

$(function() {
  $('.input').hide();
    $('#sample').change(function(){
        $('.input').hide();
        $('#' + $(this).val()).show();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="dropdown" id="sample">
  <option value="cgst">CGST</option>
  <option value="sgt">SGT</option>
</select>

<br />

<hr>

<input id="cgst" type="text" class="input" placeholder="CGST">
<input id="sgt" type="text" class="input" placeholder="SGT">

【讨论】:

    【解决方案3】:
     <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <script>
    function showRequiredOption(cval)
    {
        alert(cval);
        if(cval=='interstate')
        {
            $('#cgst').hide();
            $('#sgst').hide();
            $('#igst').show();
        }
        else if(cval=='exemptedsales')
        {
            $('#cgst').hide();
            $('#sgst').hide();
            $('#igst').hide();
    
        }
        else
        {
            $('#igst').hide();
            $('#cgst').show();
            $('#sgst').show();
    
        }
    }
    </script>
    

    您忘记添加 jquery js 文件“https://code.jquery.com/jquery-3.1.1.min.js”的链接。所以它没有用。使用它正在工作的代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多