繁体   English   中英

如何从下拉列表中获取文本框值

[英]how to take text box value from drop down list

我有一段代码,其中特定的值以下拉列表的形式出现。 我被要求删除它,并以文本框格式给出,因为还有一个值。 这是一个有时间限制的任务。 值是通过不同的方法选择的,因此它的时间阻碍了从哪里看;要获取值,我只是隐藏该选择框并以禁用形式创建一个新的文本框,然后从所选选项中获取它的值。 但是把戏不起作用

<div id='divMtdAttach' class='clsStatsDivWhole'     
     style='display:none;background:#EEF4FD;border:none;top:20px; left:40px;'>
        <div class='clsCapTblHdr' align=left valign=center     
             style='position:absolute;left:15px; font-size:9pt; font-weight:bold; width:100%;'>

             Configuration file
        </div>
<div class='clsMtdStatPnl' style='top:45px;width:90%;'>
<table>
    <tr>
        <td class='clsTblCell5' align=left valign=center>Interface &nbsp;</td>
        <td class='clsTblCell5' valign=top>
            <select id='idMethodSel' class='clsTargetSel' 
                    style='width:220px; display:none' Size=1 onchange='mtdAttachDlg("MtdAttach")'>
                    </select>

            <table class='clsTblCellTI' style='border:1px solid #7f9db9;'>
               <tr> 
                   <td id='idMethodSelTxt' 
                       style='width:210px; height:12px; padding-left:2px; padding-right:2px;border-style:none;'>
                           <script type="text/javascript">
                               showTxtbox()
                           </script>
                   </td>
               </tr>


function showTxtbox()
{
 idMethodSelTxt.innerText=idMethodSel.options[idMethodSel.selectedIndex].text;  
}

但是此技巧无效,并且会引发错误的无效对象类型。

我只是在猜你会怎样...

这是一个简单的JavaScript函数(使用jQuery),它将您的下拉菜单替换为禁用的文本字段:

function replaceSelectWithTextBox() {
    var $drop = $('#idMethodSel');
    var choice = $drop.val();
    var $textBox = $('<input type="text">');
    $textBox.val(choice).prop('disabled', true);
    $drop.parent('td').append($textBox);
    $drop.remove();
}

这个小提琴显示了一个使用10秒计时器的工作示例。

这是另一个用小提琴代替计时器的小提琴

取而代之的idMethodSelTxt.innerText ,尝试idMethodSelTxt.innerHTML

我还假设您在尝试访问它们之前,先使用document.getElementById("idMethodSelTxt")获得了idMethodSelTxtidMethodSel变量。

function showTxtbox()
{
   var idMethodSelTxt = document.getElementById("idMethodSelTxt");
   var idMethodSel = document.getElementById("idMethodSel");
   idMethodSelTxt.innerHTML = idMethodSel.options[idMethodSel.selectedIndex].text;  
}

暂无
暂无

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

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