[英]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.