【问题标题】:textbox visible on dropdown textchange jquery asp.net下拉文本更改 jquery asp.net 上可见的文本框
【发布时间】:2016-10-06 06:36:33
【问题描述】:

这是我的脚本

$(document).ready(function () {
    $("#txtfrom").hide();
    $("#txtto").hide();
    $("#txtvenue").hide();
    $("#drdsearch").text == "Venue Name"(function () {
        $("#txtfrom").hide();
        $("#txtto").hide();
        $("#txtvenue").show();
    });
    $("#drdsearch").text == "Date"(function () {
        $("#txtfrom").show();
        $("#txtto").show();
        $("#txtvenue").hide();
    });
});

这些是我的控件

<div>
    <p style="margin-top:-24px;">
        <b style="font-size:16px; margin-left:180px;">Search By:</b>
    </p>
    <select id="drdsearch" style="margin-top:-39px; margin-left:300px; height:30px; width:200px; font-size:12px;">
        <option>Default</option>
        <option>Venue Name</option>
        <option>Date</option>
    </select>
</div>
<div style="margin-left:300px">
    <p style="font-family: Verdana">
        <asp:TextBox ID="txtvenue" runat="server" CssClass="txttopborder font" ng-model="venue" Text="" placeholder="venue" Height="30" Width="200"></asp:TextBox>
        <asp:TextBox ID="txtfrom" runat="server" style="margin-left:-300px;" CssClass="txttopborder font" ng-model="from" placeholder="From Date" Text="" Height="30" Width="200"></asp:TextBox>
        <ajaxToolkit:CalendarExtender ID="clndrfrom" runat="server" TargetControlID="txtfrom" Format="dd-MM-yyyy"></ajaxToolkit:CalendarExtender>
        <asp:TextBox ID="txtto" runat="server" CssClass="txttopborder font" ng-model="to" Text="" placeholder="To Date" Height="30" Width="200"></asp:TextBox>
        <ajaxToolkit:CalendarExtender ID="clndrto" runat="server" TargetControlID="txtto" Format="dd-MM-yyyy"></ajaxToolkit:CalendarExtender>
        <%-- <button type="button" ng-click="Addrecord(x)" style="height:30px; width:50px;" class="btn btn-sm btn-primary active" data-toggle="modal" data-target="#Detailsmodel"><i class="glyphicon glyphicon-search" style="font-size:20px;"></i></button> --%>
    </p>
</div>

我正在创建一个 Web 应用程序,其中我从下拉列表中为用户提供了 3 个选项 1) 默认 2) 场地名称 3) 日期

默认情况下,Default 被选中,如果用户选择场地名称,txtvenue 应该是可见的,txtfrom,txtto 隐藏,

如果用户选择日期txtvenue 应该被隐藏并且txtfrom,`txt 可见

我在我的页面上添加了这个脚本

&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"&gt;&lt;/script&gt;

但是,所有控件都是可见的,并且下拉文本更改时没有发生任何事情。

【问题讨论】:

  • 我建议你为每个选项赋予价值

标签: c# jquery html


【解决方案1】:

使用 change() 事件。

基于所选值,您可以hideshow 尊重输入。

而且您缺少下拉选项值。

在我的例子中,

  • 我已经为下拉列表和这些值作为文本框的 id 给出了值。
  • 使用下拉值作为#id 选择器到show 相应的文本框。
  • 默认情况下,我隐藏了所有文本框,您可以根据需要进行操作。

例子

$('#select').change(function(){
   if($(this).val == "txtvenue")
   {
     //Show textboxes
   }
});

$('#drdsearch').change(function(){
   $('input').hide();
   $('#'+$(this).val()).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="drdsearch" style="margin-top:-39px; margin-left:300px; height:30px; width:200px; font-size:12px;">
        <option value="default">Default</option>
        <option value="venue">Venue Name</option>
        <option value="date">Date</option>
    </select>

<input type="text" id="default" value="default"/>
<input type="text" id="venue" value="value"/>
<input type="text" id="date" value="date"/>

【讨论】:

  • 我正在写答案,你发布了。运气不好。 1+
  • 我觉得这个脚本有问题&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"&gt;&lt;/script&gt;
  • 是的,可能是,它没有显示 404,您可以使用我在示例中使用的那个。尽量让你的 jQuery 代码更短。
  • 好的,我正在尝试这个例子
【解决方案2】:

例如,我认为您需要在 Click 上编写事件。因为这个

$(document).ready(function () {})

仅在页面准备就绪时调用

如果你想获取用户事件,你需要写这样的东西

$(document).ready(function() {
    $("#drdsearch").onChange(function(e){
        //this logic
        if( $(this).val() == "Venue Name") {
            $("#txtfrom").hide();
            $("#txtto").hide();
            $("#txtvenue").show();
        }
        if($(this).val() == "Date") {
            $("#txtfrom").show();
            $("#txtto").show();
            $("#txtvenue").hide();
        }
    })
})

【讨论】:

  • 我认为这个脚本有问题&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"&gt;&lt;/script&gt;
  • &lt;script&gt; $(document).ready(function () { $("#txtfrom").attr('readonly', true); $("#txtto").attr('readonly', true); $("#txtvenue").attr('readonly', true); }); &lt;/script&gt; 我不是 jquery 的新手(这个函数没有执行)
  • 在您的网站上尝试检查 console.log($) 这个工作?什么是输出?
  • 什么?你能解释一下它实际上是什么吗?我是 jquery 的新手
  • 打开浏览器控制台,在他进入Tab Console。并编写 console.log($)
【解决方案3】:

这里你也可以试试这个

$(document).ready(function () {
    $("#txtfrom").hide();
    $("#txtto").hide();
    $("#txtvenue").hide();
   
    
    $("#drdsearch").change(function(){
    var drp = $(this).val();
    if(drp == 1){
    $("#txtfrom").hide();
    $("#txtto").hide();
    $("#txtvenue").hide();
    }else if(drp == 2){
    $("#txtvenue").show();
    $("#txtfrom").hide();
    $("#txtto").hide();
    }else{
    $("#txtfrom").show();
    $("#txtto").show();
    $("#txtvenue").hide();
    }
    })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="drdsearch">
        <option value = "1">Default</option>
        <option value = "2">Venue Name</option>
        <option value = "3">Date</option>
    </select>

<input type="textbox" id = "txtfrom" placeholder="TxtFrom">
<input type="textbox" id = "txtto" placeholder="TxtTo">
<input type="textbox" id = "txtvenue" placeholder="Venue">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    相关资源
    最近更新 更多