使用动态下拉菜单未定义Jquery值

时间:2017-09-19 10:30:07

标签: jquery asp.net

我正在尝试构建一个字符串,每个数组值包含两个值 - 搜索项,如果它是=,>到目前为止,我已经得到了它,我完全填充了ID值,但是当我尝试通过下拉列表的ID获取值时,我得到了一个未经解决的错误。有人知道我错过了什么吗?感谢

HTML代码

<span>Where ID  </span><input name="ctl00$cBody$[ID]" type="text" id="[ID]" class="querysearch" runat="server" name="ID" /><select name="ctl00$cBody$[ID0]" id="[ID0]" class="querysearchtype">
        <option value="=">=</option>
        <option value=">">&gt;</option>
        <option value="&lt;">&lt;</option>
        <option value="&lt;>">&lt;&gt;</option>
        <option value="=&lt;">=&lt;</option>
        <option value="=>">=&gt;</option>

    </select>

Jquery代码

$('#lnkBuildWhereQuery').click(function () {
          var toPost = '';
          var stype = ' WHERE ';
          var rcount = 0
          $("#wherequery").val("")
          $('.querysearch').each(function () {
              if (rcount != 0) {
                  stype = " AND "
              };

              if (!this.value) {
                  //Blank Value Searched
              }
              else {
                  //Build dropdown id by id of textbox
                  var qid = ($(this).attr('id'))
                  //Add the dynamic number and replace the end ] to stop [id]0 - should be [id0]
                  var qidrename = "#" + qid.replace("]", rcount + "]")
                  //Stick it all together to get the value of dropdwon by id"#"
                  var qidval = $(qidrename).val()
                  //var qidval = " ="
                  toPost = toPost + stype + $(this).attr('id') + qidval + " '" + $(this).val() + '\'';
             $("#wherequery").val(toPost);
             rcount = rcount + 1}

         });
     }); 

问题部分:

var qidval = $(qidrename).val()  

1 个答案:

答案 0 :(得分:1)

您需要使用id属性来匹配#like,

$('[id=yourID]')

这里的代码需要相应地替换行

而不是

var qidrename = "#" + qid.replace("]", rcount + "]")

var qidrename = '[id="'+qid.replace("]", rcount + "]")+'"]';