0

我是 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。

4

4 回答 4

0

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

而 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 '
于 2013-08-01T13:39:38.343 回答
0

你必须“取消报价”

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

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

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

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

var myCount = parseInt(numberfromDB);

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

> '49'

> 49
于 2013-08-01T13:36:03.767 回答
0

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

$(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
于 2013-08-01T13:41:51.013 回答
0

代码很少有问题,我将通过它们:

$(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();
    }
);
于 2013-08-01T13:38:44.237 回答