2017-07-15 17 views
0

我想提醒複選框的值如果selected.And它將不會有任何ID或類。這是一個有複選框的列表框項目。用戶可以選擇多個複選框。如何使用JQUERY獲取沒有標識或類的複選框onchange的值?

這裏是我的ASPX代碼

<asp:ListBox ID="ListStoreID" CssClass="multiselect" runat="server" SelectionMode="Multiple" DataSourceID="SqlDataSource3" DataTextField="ID" DataValueField="ID"> 

</asp:ListBox> 
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:datareload33 %>" SelectCommand="SELECT [ID] FROM [Store] WHERE ([Inactive] = @Inactive)"> 
    <SelectParameters> 
      <asp:Parameter DefaultValue="false" Name="Inactive" Type="Boolean" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

<script type="text/javascript"> 
    $(function() { 
     $('[id*=ContentPlaceHolder1_ListStoreID]').multiselect({ 
      includeSelectAllOption: false, 
      buttonText: function (options, select) { 
       return 'StoreID'; 
      }, 
      buttonTitle: function (options, select) { 
       var labels = []; 
       options.each(function() { 
        labels.push($(this).text()); 
       }); 
       return labels.join(' - '); 
      } 
     }); 
    }); 
    </script> 

我使用的SqlDataSource把從數據庫列表中。所以它不會有任何id或class。

screanshot

screenshot of aspx code

client side code

更新

last screenshot

+0

你能後所呈現標記一次'.multiselect()'做它的事請。 –

+0

@KristianRoebuck請稍候。我會更新 –

+0

@KristianRoebuck你有沒有看到我的更新? –

回答

0

在jQuery中,您可以將事件綁定到元素類型或元素子元素的集合。 我會嘗試以下

$('input[type="checkbox"]').change(function() { 
    if(this.checked){ 
      //alert whatever you need.. probably this.value 
      alert(this.value); 
    } 
}); 
+0

沒有輸出。沒有警報,控制檯上沒有錯誤 –

+0

將其更改爲.click –

0

您可以只用它的節點類型中選擇一組項目。看看下面的代碼片段,選擇器將選擇元素中類multiselect-container的所有輸入。然後,您可以將「更改」事件添加到該選擇。

var checkboxes = $(".multiselect-container input"); 
 

 
checkboxes.on("change", function() { 
 

 
    console.log($(this).val() + " --> " + $(this).prop("checked")); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<ul class="multiselect-container dropdown-menu"> 
 
    <li> 
 
    <a tabindex="0"> 
 
     <label class="checkbox"> 
 
      <input type="checkbox" value="1001" />1001 
 
     </label> 
 
    </a> 
 
    </li> 
 
    <li> 
 
    <a tabindex="1"> 
 
     <label class="checkbox"> 
 
      <input type="checkbox" value="1201" />1201 
 
     </label> 
 
    </a> 
 
    </li> 
 
    <li> 
 
    <a tabindex="2"> 
 
     <label class="checkbox"> 
 
      <input type="checkbox" value="2001" />2001 
 
     </label> 
 
    </a> 
 
    </li> 
 
    <li> 
 
    <a tabindex="3"> 
 
     <label class="checkbox"> 
 
      <input type="checkbox" value="2200" />2200 
 
     </label> 
 
    </a> 
 
    </li> 
 
    <li> 
 
    <a tabindex="4"> 
 
     <label class="checkbox"> 
 
      <input type="checkbox" value="1004" />1004 
 
     </label> 
 
    </a> 
 
    </li> 
 
</ul>

編輯:

由於您使用的是jQuery插件創建checkboxes,最好是使用jQuery內置的事件代表團創造「的轉變「事件。事情是這樣的:

var container = $(".multiselect-container"); 

container.on("change", "input", function() { 

    console.log($(this).val() + " --> " + $(this).prop("checked")); 

}); 
+0

@ElchinNet我知道你的代碼應該工作。但對我來說,我不知道它爲什麼不起作用 –

+0

@ElchinNet是否因爲它在運行時創建而不提醒? –

+0

@mohamedfaiz也許你正在添加事件,並且複選框在那一刻不存在。嘗試使用'事件委託'。看看我的版本。 – ElChiniNet

0

對於這個引導,多選的jQuery上增加,有一個特定的功能來獲得所選擇的值。

下面是代碼:

<asp:ListBox ID="ListStoreID" CssClass="multiselect records" runat="server" SelectionMode="Multiple" DataSourceID="SqlDataSource3" DataTextField="ID" DataValueField="ID">      
</asp:ListBox> 

<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:recordsf1534 %>" SelectCommand="SELECT [ID] FROM [Store] WHERE ([Inactive] = @Inactive)"> 
    <SelectParameters> 
     <asp:Parameter DefaultValue="false" Name="Inactive" Type="Boolean" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

<script type="text/javascript"> 
    $(function() { 
     $('[id*=ContentPlaceHolder1_ListStoreID]').multiselect({ 
      includeSelectAllOption: false, 
      buttonText: function (options, select) { 
       return 'StoreID'; 
      }, 
      buttonTitle: function (options, select) { 
       var labels = []; 
       options.each(function() { 
        labels.push($(this).text()); 
       });     
       alert(labels.join(',')); // this alert will bring whenever user select the checkbox 
       return labels.join(' , '); 
      } 
     }); 
    }); 
</script> 
+0

查看該'jQuery'插件的文檔,如果選中*,則獲取* checkbox值的正確選項是[onChange](http://davidstutz.de/bootstrap-multiselect/#configuration-options-onChange)選項。 [buttonTitle](http://davidstutz.de/bootstrap-multiselect/#configuration-options-buttonTitle)選項不是獲取選定值的特定功能*,它是一個選項,用於設置按鈕的標題在選定的值上,它將所有選定的選項作爲參數接收(與** buttonText **選項相同) – ElChiniNet

相關問題