0

如果我想获取下拉列表的值和文本,并将其放在两个文本字段中。我只能获取值,但不能获取下拉文本。例如,单击添加行并单击下拉列表后,值和下拉文本出现在两个文本字段中。这是代码

<HTML>
    <HEAD>
    <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
    <SCRIPT language="javascript">
    function addRow(tableID) {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
        for(var i=0; i<colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
            //alert(newcell.childNodes);
            switch(newcell.childNodes[0].type) {
            case "text":
            newcell.childNodes[0].value = "";
            newcell.childNodes[0].id = "txt"+rowCount;
            break;
            case "checkbox":
            newcell.childNodes[0].checked = false;
            break;
            case "select-one":
            newcell.childNodes[0].selectedIndex = 0;
            break;
            }
        }
    }
    function deleteRow(tableID) {
        try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;
            for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
            if(rowCount <= 1) {
            alert("Cannot delete all the rows.");
            break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
            }
            }
        }catch(e) {
        alert(e);
        }
    }
    function addVal(obj,ob){
        var suf = ob.parentNode.parentNode.rowIndex;
        var txt = document.getElementById('txt'+suf);
        txt.value = obj;
    }
    </SCRIPT>
    </HEAD>

    <BODY>
    <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
    <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
    <TABLE id="dataTable" width="350px" border="1">
      <TR>
        <TD><INPUT type="checkbox" name="chk"/></TD>
        <TD><INPUT type="text" id="txt0" name="txt"/></TD>
        <TD><INPUT type="text" id="txtt0" name="txtt"/></TD>
        <TD>
          <SELECT name="country" onchange="addVal(this.options[this.selectedIndex].innerHTML,this)">
            <OPTION value="in">India</OPTION>
            <OPTION value="de">Germany</OPTION>
            <OPTION value="fr">France</OPTION>
            <OPTION value="us">United States</OPTION>
            <OPTION value="ch">Switzerland</OPTION>
          </SELECT>
        </TD>
      </TR>
    </TABLE>
    </BODY>
    </HTML>
4

2 回答 2

1

演示

将您更改<select>为:

<SELECT name="country" onchange="addVal(this)">

将您的 addRow 函数更改为:

   function addRow(tableID) {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
        for(var i=0; i<colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.innerHTML = table.rows[0].cells[i].innerHTML;

            switch(newcell.childNodes[0].type) {
            case "text":
            newcell.childNodes[0].value = "";
            if(i == 1)
            {
             newcell.childNodes[0].id = "txt"+rowCount;
            }
            else if(i==2)
            {
             newcell.childNodes[0].id = "txtt"+rowCount;
            }
            break;
            case "checkbox":
            newcell.childNodes[0].checked = false;
            break;
            case "select-one":
            newcell.childNodes[0].selectedIndex = 0;
            break;
            }
        }
    }

将您的 addVal 函数更改为:

function addVal(ob){
    var suf = ob.parentNode.parentNode.rowIndex;
    var text = ob.options[ob.selectedIndex].text;
    var value = ob.value;

    document.getElementById("txt" + suf).value = text;
    document.getElementById("txtt" + suf).value = value
}
于 2013-01-07T12:45:53.693 回答
0

您只是传递所选选项的文本,而不是值。如果您只是传递下拉对象 ob,您可以使用它来获取两个部分:

function addVal(ob){
    var suf = ob.parentNode.parentNode.rowIndex;
    var txt = document.getElementById('txt'+suf);
    txt.value = ob.options[ob.selectedIndex].innerHTML;
    var val = document.getElementById('txtt'+suf);
    val.value = ob.options[ob.selectedIndex].value;
}

<SELECT name="country" onchange="addVal(this)"></select>

您还需要整理 addRow() 以便第二个文本框与第一个文本框具有不同的 id,以便您的代码可以区分它们。具有相同 ID 的 2 个元素在技术上是无效的 html(请参阅http://www.w3schools.com/tags/att_global_id.asp中的定义和用法)

于 2013-01-07T12:35:59.087 回答