无法在选择特定选项时使用jquery显示div

时间:2017-05-24 08:01:53

标签: javascript jquery html css html-select

这是头标记

Async Function Index(name As String) As Task(Of ActionResult)
        If String.IsNullOrEmpty(name) Then
            'Dim productCategories1 = db.ProductCategories.Include(Function(x) x.ProductSubcategories)
            'Return View(Await productCategories1.ToListAsync())
            Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
        End If
        Dim productCategories = db.ProductCategories.Where(Function(x) x.Title.TitleText = name) '.Include(Function(p) p.Title).Include(Function(x) x.ProductSubcategories)
        Dim title As Title = db.Titles.Where(Function(x) x.TitleText = name).FirstOrDefault
        TempData("cat") = title.TitleText
        TempData("details") = title.Description
        Return View(Await productCategories.ToListAsync())
    End Function

这是在身体

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

这是我保存在身体外的脚本

<select name="product" id="product">
<option value="1">Select Your Product</option>    
<option value="2">On Grid</option>
<option value="3">Off Grid</option>
<option value="4">Solar Water Pump [Irrigation]</option>    
<option value="5">Solar Water Heater </option>    
<option value="6">Online UPS </option>    
<option value="7">Residential UPS</option>
<option value="8">Generator Replacement Lifts Inverter </option>
<option value="9">Energy Efficient LED'S </option>
<option value="10">Powerplant IMC Leizning with ESCOMs</option>
<option value="11">Online UPS Installation </option> 
    </select>
 <input type="file" name="file_attach[]"  />

当我选择一个问题时,它就被隐藏了。

4 个答案:

答案 0 :(得分:0)

使用.val()获取所选值

$("#product").change(function () {
    if ($(this).val() == "2") {
        $('.file_attach').css('display', 'block');
    } else {
        $('.file_attach').css('display', 'none'); 
    }
});

将类添加到元素

<input type="file" name="file_attach[]" class="file_attach" />

&#13;
&#13;
$("#product").change(function() {
  if ($(this).val() == "2") {
    $('.file_attach').css('display', 'block');
  } else {
    $('.file_attach').css('display', 'none');
  }
});
&#13;
.file_attach {
  display: none
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="product" id="product">
    <option value="1">Select Your Product</option>    
    <option value="2">On Grid</option>
    <option value="3">Off Grid</option>
    <option value="4">Solar Water Pump [Irrigation]</option>    
    <option value="5">Solar Water Heater </option>    
    <option value="6">Online UPS </option>    
    <option value="7">Residential UPS</option>
    <option value="8">Generator Replacement Lifts Inverter </option>
    <option value="9">Energy Efficient LED'S </option>
    <option value="10">Powerplant IMC Leizning with ESCOMs</option>
    <option value="11">Online UPS Installation </option> 
</select>
<input type="file" name="file_attach[]" class="file_attach" />
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您需要将类添加到输入字段:

<input type="file" name="file_attach[]" class="file_attach" />

答案 2 :(得分:0)

试试这个,

&#13;
&#13;
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>



            <select name="product" id="product">
    <option value="1">Select Your Product</option>    
    <option value="2">On Grid</option>
    <option value="3">Off Grid</option>
    <option value="4">Solar Water Pump [Irrigation]</option>    
    <option value="5">Solar Water Heater </option>    
    <option value="6">Online UPS </option>    
    <option value="7">Residential UPS</option>
    <option value="8">Generator Replacement Lifts Inverter </option>
    <option value="9">Energy Efficient LED'S </option>
    <option value="10">Powerplant IMC Leizning with ESCOMs</option>
    <option value="11">Online UPS Installation </option> 
        </select>
     <input type="file" name="file_attach[]" class="file_attach" />



          <script type="text/javascript">
      $(document).ready(function() {
        $("#product").change(function() {
           
          if ($("#product").val() == "2") {
             $('.file_attach').css('display','block');
              
          }
            else{
            $('.file_attach').css('display','none');
            }
        }); 
      });
    </script>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

3个变化:

  1. 使用.val()函数获取选择元素的值
  2. file_attach类附加到输入元素
  3. 最初隐藏输入。
  4. $(document).ready(function() {
      $("#product").change(function() {
        if ($("#product").val() === "2") {
          $('.file_attach').css('display', 'block');
    
        } else {
          $('.file_attach').css('display', 'none');
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select name="product" id="product">
    <option value="1">Select Your Product</option>    
    <option value="2">On Grid</option>
    <option value="3">Off Grid</option>
    <option value="4">Solar Water Pump [Irrigation]</option>    
    <option value="5">Solar Water Heater </option>    
    <option value="6">Online UPS </option>    
    <option value="7">Residential UPS</option>
    <option value="8">Generator Replacement Lifts Inverter </option>
    <option value="9">Energy Efficient LED'S </option>
    <option value="10">Powerplant IMC Leizning with ESCOMs</option>
    <option value="11">Online UPS Installation </option> 
        </select>
    <input type="file" class="file_attach" style="display:none" name="file_attach[]" />