【问题标题】:Check a checkbox that has a particular ID value选中具有特定 ID 值的复选框
【发布时间】:2019-01-24 08:06:27
【问题描述】:

我使用 Jquery 从数据库中检索记录,其中值和复选框附加在 div 上, 我目前希望在检索此记录时检查数据库中 ID 为 = 5 的记录。 请任何帮助

以下是检索记录并将每条记录附加到 div 中的代码。

function loadState(xxxxxxTypeDDL) {
  if ($(xxxxxxTypeDDL).val() == "") {
    clearState(xxxxxxTypeDDL);
    return;
  }

  $.ajax({
      url: "/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      global: false,
      cache: false,
      dataType: 'json',
      timeout: 30000,
      beforeSend: function() {
        $("#ajax-loading-placeholder").html('<img src="/Images/ajax-loader.gif" alt="...Loading" /> ...Loading').dialog({
          autoOpen: false,
          width: 32,
          height: 60,
          modal: true,
          resizable: false
        }).dialog("widget").find(".ui-dialog-titlebar").remove();
        // alert("Weldone Dude");
        $("#ajax-loading-placeholder").dialog('open');
        clearState(xxxxxxTypeDDL);
      },
      success: function(data) {
        $('#ajax-loading-placeholder').dialog('destroy').html('');
        var htmlToInsert = "";
        if (data == null) {
          $("<div></div>").text("No State found matching your selection criteria").dialog({
            autoOpen: false,
            modal: true,
            title: 'Select Required State',
            buttons: {
              "Ok": function() {
                $(this).dialog('destroy').html('');
              }
            }
          }).dialog("open");
          clearState(xxxxxxTypeDDL);
          $(xxxxxxTypeDDL).val("");
          $("#selected-state").hide();
          return;
        }
        $(data.State).each(function() {
          var input = "<div style='margin-bottom:5px' class='retrieved-state-list'><input type='checkbox' id='";
          input += "RetrievedState' value='";
          input += this.ID;

          if ($.inArray(this.ID.toString(), data.SelectedState) > -1) {
            input += "' checked='checked";
          }

          input += "' </input><span style='display:none'>" + this.Name + "</span>";
          input += "<span>" + this.Description + "</span></div>"
          htmlToInsert += input;
        });

【问题讨论】:

  • 我认为是因为您在这里没有正确形成输入标签。检查后您没有关闭“>”输入

标签: jquery sql-server asp.net-mvc


【解决方案1】:

对于显示信息元素中的每一行,

<div data-id="<IDs>" class="display:none"></div>

在js中,通过.attr()链接http://api.jquery.com/attr/获取这个值

JQuery.each($('div[data-id]'), function(i, val) {
   if (val.attr('data-id') == "5") {
      // Do some stuff here
   }
})

【讨论】:

  • 如果你有data-属性,最好使用$("..").data(..)`而不是.attr,因为它会更有效并且转换为正确的类型,例如&lt;div data-id="5"&gt; $("div").data("id") === 5$("div").attr("data-id") === "5"
  • 抛出 val.attr is not a function 异常
  • 对不起,错误 span[data-id] 应该更改为 div 或 css 选择器的那个区域
猜你喜欢
  • 2022-09-22
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 2011-01-14
相关资源
最近更新 更多