javascript - 根据文本自动选择组合框中的值

标签 javascript html

我有一个表,当我单击一行时,我希望将该行的值打印在有组合框的表单上。 那么问题是我只能更改组合中选项的文本而不能自动选择选项?

这是我的脚本,但它没有给我我想要的:

var fvar=document.forms.f.fonction;

    for(var i=0;i<fvar.options.length;i++){
        if (document.getElementById('listuser')
        .getElementsByTagName('tr')[row.rowIndex].cells[2].textContent==fvar.options[i].text)
            {
            fvar.options[i].selected=true;
            }
    }

HTML:

<table id="listuser">

    <thead>
        <tr>
            <th>Action</th>
            <th>Code</th>
            <th>Fonction</th>

        </tr>


        <tr id="lign" onClick="selectRowService(this)">
            <td><input type="checkbox" name="box"></td>
            <td><c:out value="${activity.cd_activite}" /></td>
            <td><c:out value="${activity.fonction}" /></td>
    </thead>


    <!-- Table body -->

    <tbody>
    </tbody>

</table>

<table id="tabmenu">

    <tr>
        <td>Fonction :</td>
        <td><div class="styled-select">
                <form:select name="fonction" path="fonction">

                    <c:forEach items="${fonc}" var="f">
                        <form:option value="${f.code_fonction}">${f.ll_fonc}</form:option>
                    </c:forEach>

                </form:select>
            </div></td>
    </tr>

</table>

请问还有其他方法可以自动选择选项值吗?

最佳答案

试试这个

var fvar=document.forms.f.fonction;

    for(var i=0;i<fvar.options.length;i++){
        if (document.getElementById('listuser')
        .getElementsByTagName('tr')[row.rowIndex].cells[2].textContent==fvar.options[i].text)
            {
                 fvar.value = fvar.options[i].value;
            }
    }

关于javascript - 根据文本自动选择组合框中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16834525/

相关文章:

jQuery KeyPress 限制我在 TextArea 上的输入

java - Jsoup http 日志记录

ajax - HTML5 历史 : Problems with back button after full page load

javascript - 强制浏览器重新加载 "cached server side html file data through javascript"

javascript - 函数中的拼接不会按预期更改字符

HTML5 - 限制文件上传

html - html表有多少个 "rows data"是 "too many"

javascript - 我应该如何在打开 SimpleModal 对话框的 BackBone.js View 中取消绑定(bind)事件?

javascript - 如何在 JavaScript 中将内容添加到动态创建的 div 中?

javascript - 使用jquery确定高度:auto and applying it to another div