簡體   English   中英

用選擇框的文本(不是值)填充文本字段

[英]Populating a text field with the text (not value) of a select box

我目前擁有填充文本字段 (id=costcenter) 的選擇框 (id=title) 的值。 當我使用選擇框的 VALUE 來觸發成本中心字段的填充時,它工作正常。 但我需要使用選擇框的 TEXT,而不是值。

這是我當前的代碼。 如何使用下拉菜單的 TEXT(而不是 VALUE)來觸發成本中心字段的填充。

謝謝大家! 瑞克

爪哇腳本:

<script language="Javascript">
var com_costcenter = new Array();

com_costcenter["Assistant Director"] = "2043000010";
com_costcenter["Clinical Specialist"] = "2044600011";
com_costcenter["Clinical Apps"] = "2044600011";
com_costcenter["select"] = "";

function setTitle(){
  var text = document.getElementsByName('title')[0].options[document.getElementsByName('title')[0].selectedIndex].value;
  document.getElementsByName('costcenter')[0].value = com_costcenter[text];
}
</script>

HTML:

<select name="title" id="title" onchange="setTitle()">
<option value="Assistant Director">Assistant Director - 2043000010</option>
<option value="Clinical Specialist">Clinical Specialist - 2044600011</option>
<option value="Clinical Apps">Clinical Applications - 2044600011</option>
</select>


<input name="costcenter" type="text" id="costcenter" size="10" maxlength="10" pattern="\d{10}" title="Enter exactly 10 digits"/>

試試.innerHTML

 var com_costcenter = new Array(); com_costcenter["Assistant Director"] = "2043000010"; com_costcenter["Clinical Specialist"] = "2044600011"; com_costcenter["Clinical Apps"] = "2044600011"; com_costcenter["select"] = ""; function setTitle(){ var title = document.getElementsByName('title')[0]; var text = title.options[title.selectedIndex].innerHTML; var number = text.split(" - ")[1]; document.getElementsByName('costcenter')[0].value = number; }
 <select name="title" id="title" onchange="setTitle()"> <option value="Assistant Director">Assistant Director - 2043000010</option> <option value="Clinical Specialist">Clinical Specialist - 2044600011</option> <option value="Clinical Apps">Clinical Applications - 2044600011</option> </select> <input name="costcenter" type="text" id="costcenter" size="25" maxlength="10" pattern="\\d{10}" title="Enter exactly 10 digits"/>

我不完全明白你在找什么,但我認為這可能會有所幫助。

<script language="Javascript">
function setTitle(){
    Title = document.getElementById('title');
    TitleText   = Title.options[Title.selectedIndex].text;
    TitleNumber = TitleText.split(" - ")[1];
    document.getElementsByName('costcenter')[0].value = TitleNumber;
}
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM