【问题标题】:jquery show/hide form field based on a variable number valuejquery根据可变数值显示/隐藏表单字段
【发布时间】:2013-08-02 11:07:27
【问题描述】:

我是 jquery 的新手,并试图根据数字值(来自数据库查询)比较在选择输入中显示/隐藏选项字段。当数据库查询中的数字达到 50 时,我需要隐藏选择选项。谷歌搜索并找到许多显示/隐藏的变体,但无法提出可行的解决方案。这是我最新的方法,它也不起作用。任何建议或可能的探索方向将不胜感激。

jQuery:

    $(document).ready(function () {
    var myCount = '50';   //will be pulled from a DB query

    $("#date-time2-1").attr("temp", 'myCount');

    if ($("#date-time2-1").val($(this).attr("temp")) > '49') {
    $("#date-time2-1").hide();
    } else {
    $("#date-time2-1").show();

    }

    );

表单html:

<h2>Example Registration Form</h2>

    <form id="form1" name="form1" method="post" action="">
      <table width="600" border="0">
        <tr>
          <td><label for="exfname">First Name</label>
          <input class="validate[required]" type="text" name="exfname" id="exfname" />
          <label for="exlname">Last Name</label>
          <input class="validate[required]" type="text" name="exlname" id="exlname" /></td>
        </tr>

        <tr>
          <td>
          <label for="datetime">Date-Session</label>
          <select class="validate[required]" name="datetime" size="1" id="datetime">
          <option value="" selected="selected">Please Select Date/Time</option>
          <option  id="date-time2-1"  value="" >Sept 3,2013 - 8:00AM</option>
          <option id="date-time1-2"  value="">Oct. 5, 2013 - 9:00AM</option>
          <option id="date-time1-1"  value="">Nov. 23, 2013 - 10:00AM</option>
          </select>
          </td>
        </tr>

        <tr>
          <td><p>
            <input  type="submit" name="submit" id="submit" value="Submit" />
            &nbsp;
            <input type="reset" name="Reset" id="Reset" value="Clear Form" />
          </p>
          </td>
        </tr>
      </table>
    </form>

谢谢。

鲍勃·P。

【问题讨论】:

  • 为什么你使用字符串49, 50 而不是数字49, 50?

标签: jquery select hide option


【解决方案1】:

你必须“取消引用”

$("#date-time2-1").attr("temp", 'myCount');

$("#date-time2-1").attr("temp", myCount);

因为你想传递一个变量而不是一个字符串

更好地将数据库查询解析为 Int 之类的

var myCount = parseInt(numberfromDB);

然后你可以像处理一个真正的整数一样处理它,也可以取消引用

> '49'

> 49

【讨论】:

    【解决方案2】:

    代码有一些问题,我将通过它们:

    $(document).ready(function () {
        var myCount = '50';   //will be pulled from a DB query
    
        //Right here, myCount shouldn't be quoted, or else your setting the attribute temp to "myCount"
        //$("#date-time2-1").attr("temp", 'myCount');
        $("#date-time2-1").attr("temp", myCount);
    
        //What is "this" referring to, the DOM ready statement? Simply compare the value like this: 
        //if ($("#date-time2-1").val($(this).attr("temp")) > 49) { //unquoted your number as well, since you're comparing numbers not strings
    
        //Or if you want to compare the temp value, use .attr("temp")
        if ($("#date-time2-1").val() > '49') {
            $("#date-time2-1").hide();
        } else {
            $("#date-time2-1").show();
        }
    );
    

    【讨论】:

    • 致所有回复的人。谢谢你。我已经从值 50 和 49 以及 myCount 中删除了引号,但仍然无法正常工作。
    • Removed the Use of $this... 将该行更改为: if ($("#date-time2-1").attr("temp") > 49) { .... . 但不起作用。
    • Aaaand? @user2640112 发生了什么
    • 我现在拥有的是这个,但它不起作用.......:$(document).ready(function () { var myCount = 50; //将从中提取数据库查询 $("#date-time2-1").attr("temp", myCount); if ($("#date-time2-1").attr("temp") > 49) { $( "#date-time2-1").hide(); } else { $("#date-time2-1").show(); } });
    • @user2640112 -- 你的控制台说什么
    【解决方案3】:

    你使用的是数字而不是字符串

    而myCount是attr中的一个变量

    var myCount = 50;//get ride of the '
    
    $("#date-time2-1").attr("temp", myCount);//get ride of the "
    
    if ($("#date-time2-1").val($(this).attr("temp")) > 49) {//get ride of the '
    

    【讨论】:

      【解决方案4】:

      让我们从让你的代码变得性感开始吧:

      $(document).ready(function () {
        var myCount = '50';   //carefull with the quotes, if your not sure about the input you can always use intval()
      
      $("#date-time2-1").attr("temp", myCount); // myCount has no quotes or else it wont work, because you dont want a string here
        if ($("#date-time2-1").val($(this).attr("temp")) > '49') { //Again same as above not a fan of the quotes
            $("#date-time2-1").hide();
        } else {
           $("#date-time2-1").show();
        }
      }); //Close the document.ready
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-23
        • 2012-06-15
        • 1970-01-01
        • 2023-03-22
        • 2016-12-22
        • 2017-02-02
        • 1970-01-01
        相关资源
        最近更新 更多