简体   繁体   English

如何在数组中添加按钮并循环它们?

[英]How to add buttons in an array and loop through them?

Got a bunch of letter buttons in code below: 下面的代码中有一堆字母按钮:

Code: 码:

<?php
    $a = range("A","Z");
?>

<table id="answerSection">
    <tr>

<?php
    $i = 1;
    foreach($a as $key => $val){
        if($i%7 == 1) echo"<tr><td>";
        echo"<input type=\"button\" onclick=\"btnclick(this);\" value=\"$val\" id=\"answer".$val."\" name=\"answer".$val."Name\" class=\"answerBtns answers answerBtnsOff\">";      
        if($i%7 == 0) echo"</td></tr>";
        $i++;
    }
?>
    </tr>
</table>

Now the code below is able to turn on an answer button: 现在,下面的代码可以打开一个答案按钮:

Code: 码:

function addwindow(btn) { 
$('#answer'+btn).addClass("answerBtnsOn");
}

But the only problem is that the code above is only able to turn on a single answer button on only. 但唯一的问题是上面的代码只能打开一个答案按钮。 For example if the "Answer" is B , then it will look for button "#answerB" and turn on that button which is button B . 例如,如果“答案”是B ,那么它将查找按钮"#answerB"并打开该按钮,即按钮B or if the "Answer" is E , then it will look for button "#answerE" and turn on that button which is button E . 或者如果“答案”是E ,那么它将查找按钮"#answerE"并打开该按钮,即按钮E

The problem is that if there are multiple answers. 问题是,如果有多个答案。 If the "Answer" is BE, then it does not turn on buttons B and E . 如果“答案”是BE,则它不会打开按钮BE This is because it is trying to find button "#answerBE" which is incorrect, it should be looking for button "#answerB" and button "#answerE" and turn them both on. 这是因为它试图找到不正确的按钮"#answerBE" ,它应该找到按钮"#answerB"并按下"#answerE"并将它们都打开。

Another example is if "Answer" is ADF, it doesn't turn on buttons A D and F because it is trying to find button "#answerADF" which is incorrect, it should be looking for button "#answerA" , button "#answerD" , and button "#answerF" and turn them all on. 另一个例子是如果“答案”是ADF,它不会打开按钮A DF因为它试图找到不正确的按钮"#answerADF" ,它应该找到按钮"#answerA" ,按钮"#answerD"和按钮"#answerF"然后全部打开。

So my question is that how can I turn on multiple buttons if there are multiple Answers? 所以我的问题是如果有多个答案我怎么能打开多个按钮? Do I need to put all the buttons in an array and loop through them so that it is able to go through all the buttons and turn on those buttons which should be turn on? 我是否需要将所有按钮放在一个数组中并循环显示它们,以便它能够通过所有按钮并打开那些应该打开的按钮?

UPDATE: 更新:

Below is the "Add" button which calls on the addwindow() function and above the add button is the "Answer" column where it displays rows of Answers 下面是调用addwindow()函数的“添加”按钮,添加按钮上方是“答案”列,其中显示了答案行

   echo '<td class="noofanswerstd">'.htmlspecialchars($searchNoofAnswers[$key]).'</td>';
    echo "<td class='addtd'><button type='button' class='add' onclick=\"parent.addwindow('$searchAnswer[$key]');\">Add</button></td></tr>";

try this: 尝试这个:

function addwindow(btn) { 
   var arr = btn.split("");
   $.each(arr, function(i, v) {
      $('#answer' + v).addClass("answerBtnsOn");
   })
}

jQuery.each()

I believe,you need this: 我相信,你需要这个:

function addwindow(btn) { 
    var answers = $.map(btn.split(''),function(chr){   return "#answer"+chr;  }).join(', ');
    $(answers).addClass("answerBtnsOn");
}

Explanation : 说明

btn.split('')   //splits btn string into char's array, 
                //  e.g. "ADE" will become ["A","D","E"]
$.map           //converts ["A","D","E"] 
                //  to ["#answerA","#answerD","#answerE"]
join(',')       //makes "#answerA, #answerD, #answerE" string 
                //  from ["#answerA","#answerD","#answerE"] array

It sounds like you are passing the 'btn' variable as a string. 听起来你正在将'btn'变量作为字符串传递。 This is acceptable if you are able to split() the string and then use a for() loop to loop through the resulting array and assign the appropriate class(es). 如果你能够split()字符串然后使用for()循环遍历结果数组并分配适当的类,这是可以接受的。

function addwindow(btn){
    var answers = btn.split(''); // leave empty if values are not separated by a delimiter
    for(var i=0; i < answers.length; i++){
        $('#answer'+answers[i]).removeClass('answerBtnsOff').addClass('answerBtnsOn');
    }
}

Depending on your delimiter, you would want to modify the following code: 根据您的分隔符,您可能希望修改以下代码:

btn.split(''); // no delimiter
btn.split(' '); // space delimiter
btn.split(','); // comma delimiter
btn.split('|'); // pipe delimiter

It also appears that you are escaping your quotes but that is not necessary if you are also using concatenation. 您似乎也在逃避引用,但如果您也在使用连接,则不需要这样做。

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings. 注意:与双引号和heredoc语法不同,特殊字符的变量和转义序列在单引号字符串中出现时不会扩展。

<?php
    $i = 1;
    foreach($a as $key => $val){
        echo ($i % 7 == 1 ? "<tr><td>" : "");
        echo '<input type="button" onclick="btnclick(this);" value="'.$val.'" id="answer'.$val.'" name="answer'.$val.'Name" class="answerBtns answers answerBtnsOff">";      
        echo ($i % 7 == 0 ? "</td></tr>" : "";
        $i++;
    }
?>

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

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