【问题标题】:Jquery find ID's that match arrayJquery查找匹配数组的ID
【发布时间】:2015-12-13 07:59:23
【问题描述】:

好的,我正在使用 map.get() 将选择的选项值存储在数组中

myArray = $("option:selected").map(function(){ return this.value }).get().join(" ");  

我有一些 div 的 ID 名称与存储在数组中的选择选项值相同

myArray = [ one two three ];

我想将 [一二三] 作为类应用到 div.box 并找到 ID 名称与 myArray [div#one div#two div#three] 相同的 Div 并获取所有输入:选中。

 <div id="one">   inputs with :checked </div>
 <div id="two">   inputs with :checked </div>
 <div id="three"> inputs with :checked </div>
 <div id="four">  inputs with :checked </div> 

如何从 ID 为 [一二三] 的 Div 中获取 :checked 项目。

我不确定如何找到与每个数组元素匹配的 ID 提前感谢您的帮助和感谢 :)

var selectedOptions = $(".select-field option:selected").map(function() {
  return this.value
}).get().join(" ");


//add selected option value as CLASS to .box
$('.box').addClass(selectedOptions);


// check if selected option stored in array match any div ID and get it's checked values

$.each(selectedOptions, function(index, value) {
  $('#' + value + ' input:checked').each(function() {
    var checked = $(this);
  });
});


//result

$("p.select").append(selectedOptions);
$("p.checked").append(checked);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select class="select-field">
  <option value="one">option one</option>
  <option value="two">option two</option>
  <option value="three">option three</option>
</select>

<select class="select-field">
  <option value="four">option four</option>
  <option value="five">option five</option>
  <option value="six">option six</option>
</select>


<div id="one">
  <p>Option one</p>
  <fieldset>
    <input type="radio" name="div1" value="left">
    <label for="left">Left</label>
    <input type="radio" name="div1" value="right" checked>
    <label for="right">Right</label>
  </fieldset>
</div>

<div id="two">
  <p>Option two</p>
  <fieldset>
    <input type="radio" name="div2" value="left" checked>
    <label for="left">Left</label>
    <input type="radio" name="div2" value="center">
    <label for="center">Center</label>
  </fieldset>
</div>

<div id="three">
  <p>Option three</p>
  <fieldset>
    <input type="radio" name="div3" value="right">
    <label for="right">Right</label>
    <input type="radio" name="div3" value="center" checked>
    <label for="center">Center</label>
  </fieldset>
</div>

<div id="four">
  <p>Option four</p>
  <fieldset>
    <input type="radio" name="div4" value="right">
    <label for="right">Right</label>
    <input type="radio" name="div4" value="center" checked>
    <label for="center">Center</label>
  </fieldset>
</div>

<div id="five">
  <p>Option five</p>
  <fieldset>
    <input type="radio" name="div5" value="left">
    <label for="left">Left</label>
    <input type="radio" name="div5" value="center" checked>
    <label for="center">Center</label>
  </fieldset>
</div>

<br />

<h4>Results</h4>
<div style="border:2px solid" class="box">Add "option one" and "option two" value as Class to this div .box example class="one four"</div>

<p class="select">Selected Options are :</p>

<p class="checked">Checked Options are :</p>

【问题讨论】:

  • 一些输入:请用var声明var,用逗号分隔数组成员,如果是字符串,请使用引号,如果检查:checked,应该有一些输入字段可以检查。
  • 仅供参考,ID 在文档上下文中必须是唯一的
  • 当然所有 ID 都是唯一的
  • @VinayKashyap 你有两次id="select-field"...

标签: jquery arrays


【解决方案1】:

您必须使用 each 遍历数组,然后您必须找到检查的值。

 $.each(myArray, function( index, value ) {
     $('#'+value+' input:checked').each(function() {
         console.log($(this));
     });
 });

这个例子可能会对你有所帮助。

【讨论】:

  • 嗨,你认为我需要表达这个或什么吗?感谢您在控制台中尝试此操作时的回复,我看到错误:未捕获的类型错误:无法使用“in”运算符进行搜索
  • 您好,您的电话可能有问题,因为我已经尝试过了,没有任何错误。
【解决方案2】:

由于您在返回的数组中使用了.join(),因此它已被转换为字符串。我建议你不要用空格.join(" ")

相反,您可以返回一个带有# 附加值的字符串,并且只返回一个不带空格的.join("'),它将输出一个逗号分隔的哈希标记值字符串。

你可以这样做:

var myArray = $("option:selected").map(function(){ 
    return '#'+this.value; 
}).get().join(","); 
// would output #one, #two, #three

现在你可以创建一个 var myArray 的选择器:

$(myArray).each(function (){
    var val = $(this).find(':ckecked').val();
    console.log(val);
});

【讨论】:

  • 这是个好主意,但我需要将选项添加为类名,因此使用 .join(" ") 和空格,然后使用 '#'+this.value 查找具有相同名称的 #
【解决方案3】:

试试这个来创建数组

var myArray = [];
$("option:selected").each(function(){
    var ThisIt = $(this);
    myArray.push($.trim(ThisIt.val()));
});

然后使用

$.each(myArray , function(i){
  var Id = $.trim(myArray[i]);
  if($('#'+Id).length > 0){
      $('#'+Id +' input[type="checkbox"]:checked').each(function(){
          alert($(this).val());
      });
  }
});

【讨论】:

  • 您好,感谢您的回复,当我在控制台中尝试此操作时,我看到错误:未捕获的类型错误:无法使用 'in' 运算符进行搜索
  • @VinayKashyap 尝试使用 .join(",");
  • 试过了,但 .join(",");你觉得我需要用这个或什么来表达吗?
  • @VinayKashyap 你能用你在控制台中使用的确切代码更新你的问题吗? Cannot use 'in' operator to search 似乎是与上述答案不同的代码段中的错误。如果您的 jQuery 版本与您的浏览器不兼容,jQuery 版本 + 浏览器版本也会有所帮助。还有你所说的“这句话”是什么意思?如果您创建了一个 JSFiddle 也会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
  • 2019-08-17
  • 1970-01-01
  • 1970-01-01
  • 2016-12-29
  • 2012-09-06
相关资源
最近更新 更多