简体   繁体   English

当文本字段的值更改时,需要与jQuery等效的JavaScript来更改下拉列表的值

[英]Need JavaScript equivalent of jQuery changing the value of a dropdown when the value of a text field changes

I am working on a Formstack form. 我正在使用Formstack表单。 I need to use Javascript to change the value of a dropdown box to whatever the value being typed into a text field is once a match is made. 一旦匹配,我需要使用Javascript将下拉框的值更改为在文本字段中键入的值。

<input type="text" id="field35497729" name="field35497729" size="50" value="" class="fsField">
<select id="field35497839" name="field35497839" size="1" class="fsField">
<option value="">&nbsp;</option>
<option value="CIPSmember">CIPSmember</option>
<option value="TECHCONNEXmember">TECHCONNEXmember</option>
<option value="TCBCpreferred">TCBCpreferred</option>
<option value="TCBCcomp2015">TCBCcomp2015</option>
</select>

So as soon as someone types in CIPSmember into the text field, the dropdown should be selected with the same value. 因此,只要有人在文本字段中CIPSmember ,就应该选择具有相同值的下拉列表。 If there is no match, the dropdown has no selection. 如果没有匹配项,则下拉列表中没有选择。

I used the following jQuery on jsFiddle, but it is not working on Formstack: 我在jsFiddle上使用了以下jQuery,但在Formstack上不起作用:

$('#field35497729').keyup( function() {
$("#field35497839").val($('#field35497729').val()); 
}
);

Here is one Javascript method I am trying on jsFiddle that does not work: 这是我在jsFiddle上尝试的一种Javascript方法,该方法不起作用:

document.getElementByID('field35497729').onkeyup = function() {
document.getElementById('field35497839').value = document.getElementByID('field35497729').value;
};

I checked here , here and maybe 10 other places but I can't get it to work. 在这里这里以及其他10个地方进行了检查,但无法正常工作。 There are plenty of tutorials on how to get a text field to change when a dropdown selection change, but not nearly as many on the opposite. 有很多关于如何在下拉菜单选择更改时使文本字段发生更改的教程,但反之则很少。

  • misspelled ID in getElementById getElementById中的 ID拼写错误
  • missing end bracket on the jQuery version jQuery版本上缺少尾括号
  • simplified to use this and $(this) 简化使用this和$(this)

I am however curious. 但是我很好奇。 Perhaps you want an autocomplete instead? 也许您想要自动完成功能

Here are your fixed versions 这是您的固定版本

Plain JS version 普通JS版本

 window.onload=function() { document.getElementById('field35497729').onkeyup = function() { document.getElementById('field35497839').value = this.value; } } 
 <input type="text" id="field35497729" name="field35497729" size="50" value="" class="fsField"> <select id="field35497839" name="field35497839" size="1" class="fsField"> <option value="">&nbsp;</option> <option value="CIPSmember">CIPSmember</option> <option value="TECHCONNEXmember">TECHCONNEXmember</option> <option value="TCBCpreferred">TCBCpreferred</option> <option value="TCBCcomp2015">TCBCcomp2015</option> </select> 


jQuery version jQuery版本

 $(function() { $('#field35497729').on("keyup",function() { $("#field35497839").val($(this).val()); // or (this.value) }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="field35497729" name="field35497729" size="50" value="" class="fsField"> <select id="field35497839" name="field35497839" size="1" class="fsField"> <option value="">&nbsp;</option> <option value="CIPSmember">CIPSmember</option> <option value="TECHCONNEXmember">TECHCONNEXmember</option> <option value="TCBCpreferred">TCBCpreferred</option> <option value="TCBCcomp2015">TCBCcomp2015</option> </select> 

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

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