Jquery 通过多个DIVs元素访问每个选定的选项值
这么说吧Jquery 通过多个DIVs元素访问每个选定的选项值,jquery,html,Jquery,Html,这么说吧 我有多个DIVs元素。 我不知道我有多少div 因为它们是由其他应用程序生成的 无论如何,由于它是纯HTML元素, 我想使用Jquery迭代每个div 因为我想访问每个div中的每个dropdownlists ///我想迭代每个div ///使用循环 对于(int i=0;i您可以尝试以下代码: $("div._Individual_Product_").each(function (index,elem) { $("select",$(this)).each(f
- 我有多个DIVs元素。
我不知道我有多少div
因为它们是由其他应用程序生成的 - 无论如何,由于它是纯HTML元素,
我想使用Jquery迭代每个div
因为我想访问每个div中的每个dropdownlists
///我想迭代每个div
///使用循环
对于(int i=0;i您可以尝试以下代码:
$("div._Individual_Product_").each(function (index,elem) {
$("select",$(this)).each(function(){
alert($(this).val());
});
});
var values = [];
$(".dynamicDropDownList").each(function (index) {
var selectValue = this.value;
values.push(selectValue);
console.log(index + " - " + selectValue);
});
var productIds = [];
$("._class_hidden_Product_ID_").each(function (index) {
var productId = this.value;
productIds.push(productId);
console.log("Product " + index + "'s id is " + productId);
});
您可以尝试以下代码:
$("div._Individual_Product_").each(function (index,elem) {
$("select",$(this)).each(function(){
alert($(this).val());
});
});
var values = [];
$(".dynamicDropDownList").each(function (index) {
var selectValue = this.value;
values.push(selectValue);
console.log(index + " - " + selectValue);
});
var productIds = [];
$("._class_hidden_Product_ID_").each(function (index) {
var productId = this.value;
productIds.push(productId);
console.log("Product " + index + "'s id is " + productId);
});
如果需要获得两个独立的值和ID数组,只需使用以下代码:
$("div._Individual_Product_").each(function (index,elem) {
$("select",$(this)).each(function(){
alert($(this).val());
});
});
var values = [];
$(".dynamicDropDownList").each(function (index) {
var selectValue = this.value;
values.push(selectValue);
console.log(index + " - " + selectValue);
});
var productIds = [];
$("._class_hidden_Product_ID_").each(function (index) {
var productId = this.value;
productIds.push(productId);
console.log("Product " + index + "'s id is " + productId);
});
如果您需要配对,可以使用以下方式:
var products = [];
$("._Individual_Product_").each(function () {
var $this = $(this);
products.push({
selectValue: $this.find(".dynamicDropDownList").val(),
productId: $this.find("._class_hidden_Product_ID_").val()
});
});
var div = [];
var dropdownList = [];
// 1. Iterate through every DIVs
$("div._Individual_Product_").each(function() {
div.push($(this));
// 2. Iterate through every dropdownlists inside each DIVs
$("select",$(this)).each(function(){
dropdownList.push($(this).val());
});
// 3. Count dropdownlists inside each DIVs
alert('DropdownList Count - ' + dropdownList.length);
dropdownList = [];
});
alert('DIVs Count - ' + div.length);
另外,您实际上可以知道有多少个div。任何jQuery对象都有length
属性,它告诉您匹配的DOM元素的确切数量。因此$(“.u-Individual\u-Product”).length
将返回所需的数量。如果需要获得两个单独的值和ID数组,您可以简单地使用以下代码:
$("div._Individual_Product_").each(function (index,elem) {
$("select",$(this)).each(function(){
alert($(this).val());
});
});
var values = [];
$(".dynamicDropDownList").each(function (index) {
var selectValue = this.value;
values.push(selectValue);
console.log(index + " - " + selectValue);
});
var productIds = [];
$("._class_hidden_Product_ID_").each(function (index) {
var productId = this.value;
productIds.push(productId);
console.log("Product " + index + "'s id is " + productId);
});
如果您需要配对,可以使用以下方式:
var products = [];
$("._Individual_Product_").each(function () {
var $this = $(this);
products.push({
selectValue: $this.find(".dynamicDropDownList").val(),
productId: $this.find("._class_hidden_Product_ID_").val()
});
});
var div = [];
var dropdownList = [];
// 1. Iterate through every DIVs
$("div._Individual_Product_").each(function() {
div.push($(this));
// 2. Iterate through every dropdownlists inside each DIVs
$("select",$(this)).each(function(){
dropdownList.push($(this).val());
});
// 3. Count dropdownlists inside each DIVs
alert('DropdownList Count - ' + dropdownList.length);
dropdownList = [];
});
alert('DIVs Count - ' + div.length);
另外,您实际上可以知道有多少个div。任何jQuery对象都有length
属性,它告诉您匹配的DOM元素的确切数量。因此$(“.u-Individual\u-Product”).length
将返回所需的数字。这是jQuery中的代码
// 1. Iterate through every DIVs
$("div._Individual_Product_").each(function() {
// 2. Iterate through every dropdownlists inside each DIVs
$("select",$(this)).each(function(){
// 3. Get each and every selected dropdown value
alert($(this).val());
});
});
您还可以获得如下所示的div和dropdownlists计数:
var products = [];
$("._Individual_Product_").each(function () {
var $this = $(this);
products.push({
selectValue: $this.find(".dynamicDropDownList").val(),
productId: $this.find("._class_hidden_Product_ID_").val()
});
});
var div = [];
var dropdownList = [];
// 1. Iterate through every DIVs
$("div._Individual_Product_").each(function() {
div.push($(this));
// 2. Iterate through every dropdownlists inside each DIVs
$("select",$(this)).each(function(){
dropdownList.push($(this).val());
});
// 3. Count dropdownlists inside each DIVs
alert('DropdownList Count - ' + dropdownList.length);
dropdownList = [];
});
alert('DIVs Count - ' + div.length);
希望这有帮助!这是jQuery中的代码
// 1. Iterate through every DIVs
$("div._Individual_Product_").each(function() {
// 2. Iterate through every dropdownlists inside each DIVs
$("select",$(this)).each(function(){
// 3. Get each and every selected dropdown value
alert($(this).val());
});
});
您还可以获得如下所示的div和dropdownlists计数:
var products = [];
$("._Individual_Product_").each(function () {
var $this = $(this);
products.push({
selectValue: $this.find(".dynamicDropDownList").val(),
productId: $this.find("._class_hidden_Product_ID_").val()
});
});
var div = [];
var dropdownList = [];
// 1. Iterate through every DIVs
$("div._Individual_Product_").each(function() {
div.push($(this));
// 2. Iterate through every dropdownlists inside each DIVs
$("select",$(this)).each(function(){
dropdownList.push($(this).val());
});
// 3. Count dropdownlists inside each DIVs
alert('DropdownList Count - ' + dropdownList.length);
dropdownList = [];
});
alert('DIVs Count - ' + div.length);
希望这能有所帮助!那么你希望选定值和隐藏值分开还是成对?那么你希望选定值和隐藏值分开还是成对?非常感谢,你的建议非常有用@Vladislav QulinThank非常感谢,你的建议非常有用@Vladislav QulinThank非常感谢,你的建议也很棒@Chinmayee G非常感谢,你的建议也很棒@Chinmayee G