简体   繁体   English

具有多个值的单选按钮逻辑

[英]Radio button logic with multiple values

I am trying to get table rows to show when the values in the cells match selctions made in two groups of radio checkboxes (already working). 我试图获取表行以显示单元格中的值何时与两组单选复选框中的选择相匹配(已工作)。

But I now want to write multiple values in a cell (ie 30, 60 and 90) in the 'term' cells. 但是我现在想在“ term”单元格的一个单元格(即30、60和90)中写入多个值。 The radio input should then show this row if 30, 60 or 90 is selected in the radio group 'term' so long as the 'amount' radio selection matches. 如果在单选组“ term”中选择了30、60或90,则单选输入将显示此行,只要“ amount”单选匹配。 This will work both ways, so there can be multiple options for both sets. 这将同时起作用,因此两组都可以有多个选项。 (ie one row will show for multiple 'amounts' values and multiple 'term' values when both radio sets match at least one from each). (即,当两个单选集都至少匹配一个时,一行将显示多个“金额”值和多个“项”值)。

 <script> function renderLenders() { var amounts = $("input[name=amount]:checked").map(function() { return $(this).val(); }).get(); var terms = $("input[name=term]:checked").map(function() { return $(this).val(); }).get(); var l = lenders.filter(function(item, index, array) { return amounts.indexOf(item.amount) != -1; }); l = l.filter(function(item, index, array) { return ( ( item.term == 30 && terms.indexOf("30") != -1 ) || ( item.term == 60 && terms.indexOf("60") != -1 ) || ( item.term == 90 && terms.indexOf("90") != -1 ) || ( item.term == 180 && terms.indexOf("180") != -1 ) || ( item.term == 360 && terms.indexOf("360") != -1 ) ); }); var rows = ""; for (var i = 0; i < l.length; i++) { rows += "<tr><td>" + l[i].name + "</td><td>" + l[i].amount + "</td><td>" + ((l[i].term > 0) ? l[i].term : "None") + "</td></tr>"; } $("#lenders").html(rows); } $(function(){ renderLenders(); $("input[type=radio]").on("click", function(){ renderLenders(); }); }) </script> 
 <input id="100" type="radio" name="amount" value="100" checked /> <label for="100"><b>&#163;100</b></label> <input id="250" type="radio" name="amount" value="250" /> <label for="250"><b>&#163;250</b></label> <input id="500" type="radio" name="amount" value="500" /> <label for="500"><b>&#163;500</b></label> <input id="1000" type="radio" name="amount" value="1000" /> <label for="1000"><b>&#163;1,000</b></label> <input id="1500" type="radio" name="amount" value="1500" /> <label for="1500"><b>&#163;1,500</b></label> <input id="30" type="radio" name="term" value="30" checked /> <label for="30"><b>30 days</b></label> <input id="60" type="radio" name="term" value="60" /> <label for="60"><b>60 days</b></label> <input id="90" type="radio" name="term" value="90" /> <label for="90"><b>3 mths</b></label> <input id="180" type="radio" name="term" value="180" /> <label for="180"><b>6 mths</b></label> <input id="360" type="radio" name="term" value="360" /> <label for="360"><b>12 mths</b></label> <table> <tbody id='lenders'></tbody> </table> <script type="text/javascript"> var lenders = [{ name: "Bank 1", amount: "100", term: 30 }, { name: "Bank 2", amount: "100", term: 60 }, { name: "Bank 3", amount: "100", term: 90 }, { name: "Bank 4", amount: "100", term: 180 }, { name: "Bank 5", amount: "100", term: 360 }, { name: "Bank 6", amount: "250", term: 30 }, { name: "Bank 7", amount: "250", term: 60 }, { name: "Bank 8", amount: "500", term: 360 }, { name: "Bank 9", amount: "1000", term: 30 }, { name: "Bank 10", amount: "1500", term: 60 }]; 

If the terms and amounts are turned into arrays, you should first declare them and then in your filter functions, you need to perform a 'sub'-filter to figure out the actual match. 如果将termsamounts转换为数组,则应先声明它们,然后在过滤器函数中,需要执行“子”过滤器以找出实际匹配项。

I have adapted your example to what I think you ask and made this work. 我已经按照您的要求调整了您的榜样,并完成了这项工作。

 var lenders = [{ name: "Bank 1", amount: ["100"], term: [30, 60] }, { name: "Bank 2", amount: ["250"], term: [60] }, { name: "Bank 3", amount: ["100", "250"], term: [30] }, { name: "Bank 4", amount: ["100"], term: [60] }, { name: "Bank 5", amount: ["250"], term: [30] }, { name: "Bank 6", amount: ["100", "250"], term: [30, 180] } ]; function renderLenders() { var amounts = $("input[name=amount]:checked").map(function() { return $(this).val(); }).get(); var terms = $("input[name=term]:checked").map(function() { return $(this).val(); }).get(); var l = lenders .filter(function(item, index, array) { return item.amount.filter(function(a) { return amounts.indexOf('' + a) >= 0; }).length; }) .filter(function(item, index, array) { return item.term.filter(function(t) { return terms.indexOf('' + t) >= 0; }).length; }); var rows = ""; for (var i = 0; i < l.length; i++) { rows += "<tr><td>" + l[i].name + "</td><td>" + l[i].amount + "</td><td>" + ((l[i].term > 0) ? l[i].term : "None") + "</td></tr>"; } $("#lenders").html(rows); } $(function() { renderLenders(); $("input[type=radio]").on("click", function() { renderLenders(); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label> <input type="radio" name="amount" value="100" checked /> <b>&#163;100</b> </label> <label> <input type="radio" name="amount" value="250" /> <b>&#163;250</b> </label> <label> <input type="radio" name="term" value="30" checked /> <b>30 days</b> </label> <label> <input type="radio" name="term" value="60" /> <b>60 days</b> </label> <table> <tbody id='lenders'></tbody> </table> 

If your logic requires to show only the actual matching values, I'd change the filtering logic to first map the new values and then filter on these For example 如果您的逻辑只需要显示实际的匹配值,我将更改过滤逻辑以首先映射新值,然后根据这些值进行过滤。例如

var l = lenders
  .map(function(item) {
    return {
      name: item.name,
      amount: item.amount.filter(function(v) {
        return amounts.indexOf('' + v) >= 0;
      }),
      terms: item.term.filter(function(v) {
        return terms.indexOf('' + v) >= 0;
      })
    };
  })
  .filter(function(item) {
    return item.amount.length && item.terms.length;
  });

I've created a fiddle on what I would really do , this also includes the mapping and reduces the amount of logic required to compare as the example makes use of radio buttons ("there can only be one" selected), so it is useless to create an array for it. 我已经在实际操作中创建了一个小提琴 ,其中还包括了mapping并减少了比较所需的逻辑量,因为该示例使用了单选按钮(“只能有一个”被选中),因此它是无用的为它创建一个数组。 Unless you're switching to checkbox elements, then you must resort to the use of arrays. 除非切换到checkbox元素,否则必须诉诸使用数组。

By now you may or may not have noticed I also touched your HTML example, here's why: 到目前为止,您可能已经注意到,也可能尚未注意到我也触摸了您的HTML示例,这是为什么:

<input id="100" .. /><label for="100">..</label>

An id value cannot start with a number (so it cannot be a number), and for the demonstrated use case, it is redundant to go through lengths to apply references while it is just as easy to simply embed the <input> inside the <label> and enjoy the same interaction mechanics it offers. id值不能以数字开头(因此它不能是数字),对于演示的用例,遍历长度以应用引用是多余的,而将<input>嵌入在<label>并享受与它相同的交互机制。 The only reason (for me) to not use this approach is when the <label> is not anywhere near the <input> , then I'd link them with id / for . (对我而言)不使用此方法的唯一原因是<label>不在<input>附近的任何地方,然后将它们与id / for链接。

Some final thoughts my (small) change to indexOf . 我(小的)最后一些想法变成了indexOf

You may have noticed I write the indexOf logic slightly different. 您可能已经注意到,我编写的indexOf逻辑略有不同。 The >= 0 instead of !== -1 is a personal preference, both do the same thing. >= 0代替!== -1是个人喜好,两者都做相同的事情。 I like to think of >= 0 as "is it in the array?" 我喜欢将>= 0视为“它在数组中吗?” , while !== -1 (to me) reads as "is it not not in the array?" ,而!== -1 (对我来说)读为“不是不在数组中吗?” . I have also made sure I'm comparing to the string values ( indexOf('' + t) ), I do this because it allows me to ensure the types match (any value obtained from HTML-attributes is a (DOM)String) whilst providing the option to define them in the lenders as convenient as possible (numbers tend to be 2 characters shorter than strings). 我还确保要与字符串值( indexOf('' + t) )进行比较,因为这样做可以确保类型匹配(从HTML属性获取的任何值都是(DOM)String)同时提供了尽可能方便地在lenders定义它们的选项(数字通常比字符串短2个字符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM