简体   繁体   English

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

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

i have a chunk of code in which a particular value comes in form of drop down list . 我有一段代码,其中特定的值以下拉列表的形式出现。 i have been asked to remove that and give in text box format as there is also a single value. 我被要求删除它,并以文本框格式给出,因为还有一个值。 it's a task with time limit . 这是一个有时间限制的任务。 the value is getting selected by different method so its time hindering to see from where it;'s taking value i simply hide that select box and make a new text box in disabled form and taking its value from selected option. 值是通过不同的方法选择的,因此它的时间阻碍了从哪里看;要获取值,我只是隐藏该选择框并以禁用形式创建一个新的文本框,然后从所选选项中获取它的值。 but trick is not working 但是把戏不起作用

<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;  
}

but this trick is not working and its throwing an error invalid object type. 但是此技巧无效,并且会引发错误的无效对象类型。

I'm just guessing at what you're afer... 我只是在猜你会怎样...

Here's a simple JavaScript function (using jQuery) that replaces your dropdown with a disabled text field: 这是一个简单的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();
}

This fiddle shows a working example using a 10 second timer. 这个小提琴显示了一个使用10秒计时器的工作示例。

Here's another fiddle that uses a button in stead of a timer. 这是另一个用小提琴代替计时器的小提琴

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

I am also assuming you got the idMethodSelTxt and idMethodSel variables with document.getElementById("idMethodSelTxt") before trying to access them. 我还假设您在尝试访问它们之前,先使用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