简体   繁体   English

从 select 上的另一个输入框分配输入框值 点击 jquery

[英]Assign input box value from another input box on select click in jquery

I have a form where there are 5 fields, 3 of them has a value like hours,days and lease which has default values, then one selectbox and another empty input box, my code is like below:我有一个表单,其中有 5 个字段,其中 3 个字段的值如小时、天和租约,具有默认值,然后是一个选择框和另一个空输入框,我的代码如下所示:

 $(document).ready(function() { $('#hr').change(function() { $('#mytexts').val($('#hours').val()); }); });
 <input type="text" id="hours" value="23" readonly/> <input type="text" id="days" value="33" readonly/> <input type="text" id="lease" value="33" readonly/> <select name="hours" id="hr"> <option value="" onchange="myFunctionhours(this)">---Select---</option> <option value="hours">Hours</option> <option value="days">Days</option> <option value="lease">Lease</option> </select> <input type="text" name="crate" id="mytexts" value="" readonly/>

When a value is selected From the selectBox I want to empty input box to get values from the 3 input boxes accordingly, now in what I did I am able to only get the value of hours box, can anyone please tell me how to do this, thanks in advance当从selectBox中选择一个值时,我想清空输入框以相应地从3个输入框中获取值,现在我所做的只能获取小时框的值,谁能告诉我该怎么做, 提前致谢

you have to take the the value from the change event, by useing event.target.value then assign its follwing value to the result input as follow:您必须通过使用event.target.value从更改事件中获取值,然后将其以下值分配给结果输入,如下所示:

  $('#hr').change(function(event) {
    let selected = event.target.value;
     if(selected) $('#mytexts').val($("#"+selected).val());
  });

see below wokring snippet:请参阅下面的工作片段:

 $(document).ready(function() { $('#hr').change(function(event) { let selected = event.target.value; $("#choise").html(selected) if(selected) $('#mytexts').val($("#"+selected).val()); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="hours" value="23" readonly/> <input type="text" id="days" value="33" readonly/> <input type="text" id="lease" value="33" readonly/> <br /><br /> <select name="hours" id="hr"> <option value="" onchange="myFunctionhours(this)">---Select---</option> <option value="hours">Hours</option> <option value="days">Days</option> <option value="lease">Lease</option> </select> <br /><br /> <input type="text" name="crate" id="mytexts" value="" readonly/> <span id="choise"></span>

You have to use the value of dropdown value as query selector in JQuery.您必须使用下拉值的值作为 JQuery 中的查询选择器。

 $(document).ready(function() { $('#hr').change(function() { $('#mytexts').val($("#"+$('#hr').val()).val()); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <:-- begin snippet: js hide: false console: true babel: null -->

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

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