简体   繁体   English

将javascript填充的下拉列表中的所选项目添加到文本框

[英]Adding selected item from a drop down populated by javascript to a text box

Hi I have a drop down whose items are populated by javascript and has no option tags. 嗨,我有一个下拉列表,其中的项目由javascript填充,没有选项标签。 When the page loads, the drop down is loaded with all the options by an external JavaScript file. 页面加载后,下拉菜单会通过外部JavaScript文件加载所有选项。 When the user selects an item from this drop down i would like this selected item to update a text box (which is on the same page) with their selection so I can then send this by email. 当用户从该下拉列表中选择一个项目时,我希望该选择的项目使用他们的选择来更新文本框(在同一页面上),这样我就可以通过电子邮件发送该文本框了。

I can get it to work on a normal drop down with actual options tags that isn't populated by javascript, but can't get it to work with my Populated drop down. 我可以使用未由javascript填充的实际选项标签在正常下拉菜单中使用它,但不能使其与我的填充下拉列表一起使用。 I have read on various posts that drop down items populated with JavaScript wont be read by the server because it is all done client side, and that a work around involves adding the populated drop down selection to a hidden field, but I cant find a method for doing so. 我已经读过各种文章,这些文章将删除用JavaScript填充的下拉列表项,因为它们都是在客户端完成的,因此服务器不会读取它,并且解决方法是将填充的下拉列表项添加到隐藏字段中,但是我找不到方法为此。 I am also working with classic ASP to send the form 我也在使用经典的ASP发送表格

Hope someone can help, 希望有人能帮忙

Hi I forgot to mention that the drop down inst populated by inline JavaScript but rather from an external JS file 嗨,我忘了提到下拉菜单由内联JavaScript填充,而是由外部JS文件填充

hi some code i have used, works fine for a normal drop down, but not my one that is populated with JS 嗨,我使用过一些代码,对于正常的下拉列表来说效果很好,但对于我的使用JS填充的代码却不起作用

function ChooseContact(data) {

document.getElementById ("friendName").value = data.value;

}


<select onchange="ChooseContact(this)"><option value='1'>1</option><option  value='2'>2</option></select>

Thanks R 谢谢R

HI - its all done using this external script http://www.dynamicdrive.com/dynamicindex1/chainedmenu/index.htm HI-使用此外部脚本http://www.dynamicdrive.com/dynamicindex1/chainedmenu/index.htm完成所有操作

HI Here is a snippit of the exteranl JS file populating the drop down HI这是exteranl JS文件的摘录,其中包含下拉列表

/var hide_empty_list=true; //uncomment this line to hide empty selection lists

var disable_empty_list=true; var disable_empty_list = true; //uncomment this line to disable empty selection lists //取消注释此行以禁用空的选择列表

var onclickaction="alert" //set to "alert" or "goto". var onclickaction =“ alert” //设置为“ alert”或“ goto”。 Former is for debugging purposes, to tell you the value of the final selected list that will be used as the destination URL. 前一个用于调试目的,用来告诉您最终选定列表的值,该列表将用作目标URL。 Set to "goto" when below configuration is all set up as desired. 根据需要全部设置以下配置时,设置为“ goto”。

var newwindow=0 //Open links in new window or not? var newwindow = 0 //是否在新窗口中打开链接? 1=yes, 0=no. 1 =是,0 =否。

/////DEFINE YOUR MENU LISTS and ITEMS below///////////////// //////在下面定义您的菜单列表和项目//////////////////

addListGroup("chainedmenu", "First-Select");

addOption("First-Select", "Select a course type", "", 1); //HEADER OPTION
addList("First-Select", "New Staff", "", "NewStaff");
addList("First-Select", "All Staff", "", "AllStaff");
addList("First-Select", "Learning, Teaching, Research and Knowledge Exchange", "",   "Learning");

addList("First-Select", "Leaders and Managers", "", "Leaders"); addList(“ First-Select”,“ Leaders and Managers”,“”,“ Leaders”);

With your posted code, it should be perfectly possible to achieve what you are trying to do. 使用您发布的代码,完全有可能实现您要执行的操作。 I've constructed an example that expands on your code to show you how the input field can be updated. 我构建了一个扩展代码的示例,向您展示如何更新输入字段。

See this jsFiddle 看到这个jsFiddle

And also one that shows you it should still work if all of the options are generated via JavaScript. 并且还向您展示了如果所有选项都是通过JavaScript生成的,它仍然可以正常工作。 So maybe the issue you are having is elsewhere? 因此,也许您遇到的问题在其他地方?

<select id="selecty" onchange="ChooseContact(this)"></select>
<input type="text" id="friendName" />

<script>
    var opt = document.createElement("option");
    opt.text = "One";
    opt.value = "1";
    document.getElementById("selecty").add(opt,null);

    opt = document.createElement("option");
    opt.text = "Two";
    opt.value = "2";
    document.getElementById("selecty").add(opt,null);

    opt = document.createElement("option");
    opt.text = "Three";
    opt.value = "3";
    document.getElementById("selecty").add(opt,null);

    function ChooseContact(data) {
        document.getElementById("friendName").value = data[data.selectedIndex].value;
    }
</script>

See this jsFiddle 看到这个jsFiddle

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

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