简体   繁体   English

更改只读输入值 <select>期权变更

[英]Change ReadOnly Input Value on <select> option change

I want to display the value in a "select > option" in an based on the location selected from my tag. 我想根据从标签中选择的位置,在“选择>选项”中显示值。

Here is the html: 这是html:

<select name="stateCoord" id="stateCoord" autocomplete="off">
       <option selected="selected">SELECT STATE</option>
       <option value="lg">Lagos</option>
       <option value="abj">Abuja</option>
</select>

<input type="text" name="stateCoordInfo" value="" id="stateCoordInfo"  readonly>

And here is the javascript: 这是javascript:

$(document).ready(function() {

$("#stateCoord option").filter(function() {
    return $(this).val() == $("#stateCoordInfo").val();
}).attr('selected', true);

$("#stateCoord").live("change", function() {

    $("#stateCoordInfo").val($(this).find("option:selected").attr("value"));
});
});

I'm still getting the hang on javascript so a detailed explanation won't hurt. 我仍然对javascript有所了解,因此详细的说明不会受到影响。 Thanks in advance. 提前致谢。

I am not sure, if this is what you need, but I think it should be: JSfiddle 我不确定这是否是您需要的,但是我认为应该是: JSfiddle

Here is the JS code: 这是JS代码:

$(document).ready(function() {

  $("#stateCoord").change(function() {
      var value = $("#stateCoord option:selected").text();
      $("#stateCoordInfo").val(value);
  });
});
$(document).ready(function() {

     $("body").on("change","#stateCoord",function() {

     $("#stateCoordInfo").val($(this).find("option:selected").val());

});

   $("#stateCoord").trigger("change");
});

From what I understand, you want to update the text shown in the readonly text box with the value attribute of the selected option from the dropdown. 据我了解,您想使用下拉菜单中所选选项的value属性更新readonly文本框中显示的文本。

To do this, you just need to update the val of your read-only text box to the val of the drop-down whenever change event is fired. 要做到这一点,你只需要更新val您的只读文本框的val每当下拉的change事件。

 $(document).ready(function() { $('#stateCoord').change(function(e) { $('#stateCoordInfo').val($(this).val()); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select name="stateCoord" id="stateCoord" autocomplete="off"> <option selected="selected">SELECT STATE</option> <option value="lg">Lagos</option> <option value="abj">Abuja</option> </select> <input type="text" name="stateCoordInfo" value="" id="stateCoordInfo" readonly> 

If I assume correctly, you want this: 如果我假设正确,那么您需要这样做:

1) initialization of the select option at document ready, that strange thing the code with filter() was trying to do. 1)在文档准备就绪时初始化select选项,那是带有filter()的代码试图做的奇怪的事情。

2) Capture the change event on the select and show the value of the current selected option on the input. 2)select上捕获change事件,并在输入上显示当前选择的选项的值。 This is what the live() ( deprecated ) part was trying to do. 这是live()已弃用 )部分试图做的事情。

 $(document).ready(function() { $("#stateCoord").val("").change(); $("#stateCoord").change(function() { $("#stateCoordInfo").val($(this).val()); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select name="stateCoord" id="stateCoord"> <option value="" selected>SELECT STATE</option> <option value="lg">Lagos</option> <option value="abj">Abuja</option> </select> <input type="text" name="stateCoordInfo" value="" id="stateCoordInfo"> 

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

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