简体   繁体   English

从选中的父行中获取元素

[英]Get elements from Parent Row of checked checkboxes

I have the following row in a table. 我在表中有以下行。

<tr class="data_rows" ng-repeat='d in t2'> <td class="tds"> <input class='checkBoxInput' type='checkbox' onchange='keepCount(this)'></td> <td class="tds"><a href='perf?id={{d.ID}}'>{{d.ID}}</a></td> <td class="tds">{{d.HostOS}}</td> <td class="tds">{{d.BuildID}}</td> <td class="tds">{{d.Description}}</td> <td class="tds">{{d.User}}</td> <td class="tds">{{d.StartTime}}</td> <td class="tds">{{d.UniqueMeasure}}</td> <td class="tds">{{d.TotalMeasure}}</td> </tr>

Here's the HTML for button that will invoke the function to collect the ids from checked check boxes and store them. 这是HTML for按钮,它将调用该函数以从选中的复选框中收集ID并将其存储。

<div id='compButtonDiv' align='center' style="display: none;"> <input id='cButton' type='button' value='compare selections' onclick='submitSelection()' style= "margin :0 auto" disabled> </div>

The data is in t2 which consists of an array of length 15-20. 数据在t2中,它由长度为15-20的数组组成。 What i want to do is get the value of ID ie, {{d.ID}} of the 2 checked check boxes so that i can store them in a variable and pass them as query parameters to URL using `location.href = url?param1&param2' 我想做的是获取ID的值,即2个选中复选框的{{d.ID}} ,以便我可以将它们存储在变量中,并使用`location.href = url将其作为查询参数传递给URL ?param1&param2'

Here's the javascript: 这是JavaScript:

function keepCount(obj){     
debugger;
//var count=0;
if(obj.checked){
    obj.classList.add("checked");
}else{
    obj.classList.remove("checked");
}

var count = document.getElementsByClassName("checked").length;
var cBtn = document.getElementById('cButton');
//alert(count);

if(count == 2){
    cBtn.disabled = false;
}
else if(count < 2){
    cBtn.disabled= true;
}
else{
    cBtn.disabled= true;
    alert("Please Select two sets for comparison. You have selected: " +     count);
}
}

function submitSelection(){
// what should be the code here??

location.href= "existingURL?a&b";
}

Now can someone please tell me how to get the id's?? 现在有人可以告诉我如何获取ID的信息吗? I need to extract ID from the checkboxes that are checked(on the click of button whose code i've mentioned above'. 我需要从选中的复选框中提取ID(单击我上面提到的代码按钮)。

Thanks. 谢谢。

-Ely -伊利

Firstly when we use angularjs we tend to depend less and less on DOM manipulation. 首先,当我们使用angularjs时,我们倾向于越来越少地依赖DOM操作。

For this reason, what you can do is to attach ngModel to the checkbox. 因此,您可以将ngModel附加到复选框。 Like: 喜欢:

<input class='checkBoxInput' ng-model='d.isChecked' type='checkbox' onchange='keepCount(this)'>

What this does is, it attaches the variable (in your case the property of item in the list) to the check box. 这是将变量(在您的情况下为列表中item的属性)附加到复选框的作用。 If it is checked it is true, if unchecked, initially it will be undefined, later on checking and then unchecking it will be false. 如果选中,则为true;如果未选中,则最初将为undefined,稍后再进行检查,然后取消选中为false。

Now, when you submit, just loop over the original list in the function and check the values of d.isChecked (true/falsy values). 现在,当您提交时,只需在函数中的原始列表上循环并检查d.isChecked的值(真/假值)。 Then you can add the necessary items in a separate list for submission. 然后,您可以将必要的项目添加到单独的列表中以进行提交。 The only concern is when checking the list on submission , check if(d.isChecked), so that it ignores the falsy values(false/undefined). 唯一需要关注的是在检查提交列表时,检查if(d.isChecked),以便它忽略虚假值(false / undefined)。

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

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