如何在javascript中验证动态创建的选择框?

时间:2012-02-24 05:40:30

标签: javascript dom

我有一个html表,其中的行是通过javascript函数动态添加的。我有一个选择框来选择每行中的产品名称。它的工作正常。

现在我要验证选择框。我不知道怎么做。在即将到来的行中,不应多次选择文本框中的值。

我想在选择时显示警告信息,例如“您选择的产品已被选中”,并将所选值更改为 - 选择---。

action here

中查看

1 个答案:

答案 0 :(得分:0)

假设您不希望允许多个选择框保持相同的值。 在insSpec()方法中,您可以迭代现有行,即选择框并捕获这些值。然后,您可以使用所有选择框的selected属性检查是否已选中。 / p>

更新

var table = document.getElementById("mytab1");
for (var i = 0, cell; cell = table.cells[i]; i++) {
     //iterate through cells
     for (var j = 0, cell2; cell2 = table.cells[j]; j++) {
     //check already selected selectboxes have same value or not
     //compare only selected select boxes
     if(cell.getElementsByTagName('select')[0].selected && cell2.getElementByTagName('select')[0].selected){
         //compare values of both cell's select boxes using value property and alert message                  
    }         
}

请不要直接复制粘贴此代码。这只是一种方法。

Reference