简体   繁体   English

通过使用具有相同ID的多行更改options菜单中的值来选中复选框

[英]Check the checkbox by changing values in optionsMenu using Multiple rows with same id

I am creating a Data table with following code 我正在使用以下代码创建数据表

<?php 
foreach($redeemSales as $sale)
{ 
   ?>
   <tr class='clickableRow<?php echo $isRead ? '' : ' newrow' ?>' href='#'>
       <td><form><input type="checkbox" id="userSelection" name="userslection" ></form></td>
      <td><?php echo $sale["ring"];?></td>
      <td><?php echo formatFullDate($sale["soldDate"]) ?></td>
      <td><?php echo $sale["saleType"]; ?></td>
      <td>   
          <div class="col-lg-8">
              <select name="type" id="redeemOptions" class="form-control">
                <option value="None">None</option>
                <option value="CD">(CD)</option>
                <option value="Amex">American Express Card (Amex)</option>
            </select>
        </div>
      </td>
  </tr>
<?php }?>

I want to make it so if anyone change the option to any oe of CD Or Amex, it will set the selection to its row as checked. 我要这样做,以便如果有人将选项更改为CD或Amex的任何oe,它将按选择将其设置设置为该行。

jAVAScript code is here jAVAScript代码在这里

<script language="javascript">
$(document).ready(function(e) 
{    
    $('#redeemOptions').change(function(){
        if($('#redeemOptions').val() == 'None') 
        {           
            document.getElementById("userSelection").checked = false;
        }
        else
        {
            document.getElementById("userSelection").checked = true;   
        } 
    });
});

</script>

As you can see that there is a for loop, so its rows getting added in a table. 如您所见,有一个for循环,因此它的行被添加到表中。 The above method works only for first row. 上面的方法仅适用于第一行。 If i change the options, it will set selection to Checked. 如果我更改选项,它将选择设置为“已检查”。 But after first rows, no other row is showing that behavior. 但是在第一行之后,没有其他行显示该行为。 It is probably something to do with id of the elements. 这可能与元素的id有关。

How can one find out a solution so that other rows show same behavior. 如何找到一种解决方案,以便其他行显示相同的行为。 Secondly, if I have to get the ids or values of all rows that are "Checked" how will i do it in php 其次,如果我必须获取所有“已检查”的行的ID或值,我将如何在php中进行操作

the problem is that an id has to identify one single element on the page, and you are generating multiple items with the same id. 问题是,一个ID必须标识页面上的一个元素,并且您正在生成具有相同ID的多个项目。 Change your select to something like the following: 将您的选择更改为以下内容:

<select name="type" class="form-control redeemOptions">

And then change your javascript function to the following, to take advantage of the fact that "this" will equal the object that the change event is being fired from: 然后将您的javascript函数更改为以下代码,以利用以下事实:“ this”将等于触发change事件的对象:

$('.redeemOptions').change(function(){
    var menuChanged = $(this),
        parentForm = menuChanged.closest('form'),
        correspondingCheckbox = parentForm.find('input[name=userSelection]');
    if(menuChanged.val() == 'None') 
    {           
        correspondingCheckbox[0].checked = false;
    }
    else
    {
        correspondingCheckbox[0].checked = true;   
    } 
});

Finally, remove the id="userSelection" from the checkbox and just leave the name. 最后,从复选框中删除id =“ userSelection”并保留名称。 It won't hurt the functionality but it is technically invalid because again you can only have one element of a given id on the page. 它不会损害功能,但是在技术上是无效的,因为同样,页面上只能包含一个具有给定ID的元素。

As I can see for every $sale in $redeemSales you have a checkbox having id "userSelection" and a select box having id "redeemOptions". 如我所见,对于$ redeemSales中的每个$ sale,都有一个ID为“ userSelection”的复选框和一个ID为“ redeemOptions”的选择框。 I advise you to use unique ids. 我建议您使用唯一的ID。 So append $i=0...$i++ to the ids of both. 因此,将$ i = 0 ... $ i ++附加到两个ID上。

For Row 1: userSelection0 redeemOptions0
For Row 2: userSelection1 redeemOptions1
and so on..

In Select tag, use onchange event :- 在“选择”标签中,使用onchange事件:-

<select name="type" id="redeemOptions<?php echo $i; ?>" class="form-control" onchange="foo(<?php echo $i; ?>)">

And write a javascript function :- 并编写一个javascript函数:-

<script language="javascript" type="text/javascript">
function foo(id)
{    
    if($('#redeemOptions'+id).val() == 'None') 
    {           
        document.getElementById("userSelection"+id).checked = false;
    }
    else
    {
        document.getElementById("userSelection"+id).checked = true;   
    } 
}
</script>

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

相关问题 如何从具有相同类、类型、值和选项卡索引的多个复选框中选择一个复选框并且复选框 ID 不断变化 - How to select a checkbox from multiple check boxes having same class,type,value and tab index And checkbox id keeps on changing 通过更改不同行中选项的值来选中/取消选中复选框 - Check/Uncheck the checkbox by changing values in options in different rows 更改具有相同 id 的多个元素 - Changing multiple elements with same id 多行自动填充,具有相同的ID - Autocomplete in multiple rows, same id 使用ID启用多个复选框 - Enable the multiple checkbox using Id 我如何抓取 HTML 复选框表单值并检查 ID 的值是否与 User_id 相同以自动增加投票 - How do i grab HTML Checkbox Form Values and check if the Value of the ID is the same as the User_id to auto increment Votes 单击DIV时,选中多个复选框(不具有相同名称) - On DIV click, check multiple checkbox (Not with same name) 使用一个复选框选择对表进行多次更改 - 使用 id 和 class 更改一个 td 的类和父 tr 的样式 - multiple changes to a table with one checkbox selection - changing class of one td and style of parent tr using id and class 科尔多瓦Android JavaScript检查多个复选框值 - Cordova Android javascript to check multiple checkbox values JavaScript在同一个ID上有多个值 - Javascript multiple values on the same ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM