简体   繁体   English

"获取select2中选定选项的值,然后使用jquery将值设置为输入文本?"

[英]Get the value of selected option in select2 and then set the value to an input text using jquery?

I'm new to jquery and javascript, so maybe this is a silly question but i'm having problems setting the value obtained from a selected option in select2 to a text input.我是 jquery 和 javascript 的新手,所以也许这是一个愚蠢的问题,但我在将 select2 中的选定选项获得的值设置为文本输入时遇到问题。 This is my code:这是我的代码:

  <head>

    <!-- stylesheets -->
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css">


    <!-- scripts -->
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

    <script>
      $(function(){
        // turn the element to select2 select style
        $('.select2').select2({
          placeholder: "Select a state"
        });

        var data = $(".select2 option:selected").text();
        $("#test").val(data);
      });



    </script>
  </head>

  <body>

    <p>select2 select box:</p>
    <p>
      <select  class="select2" style="width:300px">
          <option> </option>
          <option value="AL">Alabama</option>
          <option value="VT">Vermont</option>
          <option value="VA">Virginia</option>
          <option value="WV">West Virginia</option>
      </select>
    </p>

    <input type="text" id="test"> 

  </body>

  </html>

You are almost there.你快到了。 Put your value assignments within the change handler of the dropdown.将您的值分配放在下拉列表的change处理程序中。 The way you have it, it's just getting called when the page is loaded.你拥有它的方式,它只是在页面加载时被调用。

  $(function(){
    // turn the element to select2 select style
    $('.select2').select2({
      placeholder: "Select a state"
    });

    $('.select2').on('change', function() {
      var data = $(".select2 option:selected").text();
      $("#test").val(data);
    })
  });

As per the docs https://select2.org/programmatic-control/events#event-data根据文档https://select2.org/programmatic-control/events#event-data

$('.select2').on('select2:select', function (e) {
    var data = e.params.data;
    $('#test').val(data.text);
});

You can use change event : whenever an option is selected the value can be copied to the text box:您可以使用更改事件:只要选择了一个选项,就可以将值复制到文本框:

 $(function(){ // turn the element to select2 select style $('.select2').select2({ placeholder: "Select a state" }).on('change', function(e) { var data = $(".select2 option:selected").text(); $("#test").val(data); }); });
 <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css"> <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script> <p>select2 select box:</p> <p> <select class="select2" style="width:300px"> <option> </option> <option value="AL">Alabama</option> <option value="VT">Vermont</option> <option value="VA">Virginia</option> <option value="WV">West Virginia</option> </select> </p> <input type="text" id="test">

simply you can do this:你可以这样做:

$('#your-select-id').on("change", function(e) {
                console.log($("#your-select-id").val())
 });
$("#e15").on("change", function () {
    $("#e15_val").html($("#e15").val());
});

I am adding this here because, a couple of months back, the answers provided here worked for me, but now, some voodoo happened and the only way for me to get the value of a selected item was doing the following:我在这里添加这个是因为,几个月前,这里提供的答案对我有用,但是现在,发生了一些伏都教,我获得所选项目价值的唯一方法是执行以下操作:

$('.findUser').select2({
    minimumInputLength: 3,
    placeholder: "Search Members...",
    allowClear: true,
    dropdownAutoWidth : true,
    width: '250px'
}).on('change', function (e) {
    console.log(e.val);
});

As you can see, I used e.val to get the value.如您所见,我使用e.val来获取值。 The only way I could get the trigger of the selected value to work was using change since using select2:select simply would not work (Which it did a couple of months back if I reckon).我可以让所选值的触发器起作用的唯一方法是使用change因为使用select2:select根本不起作用(如果我估计它在几个月前就做了)。 To debug and get to this point I had to do a console.log(e) to get all events when it triggered the on change case.为了调试并达到这一点,我必须执行console.log(e)以获取触发 on change 案例时的所有事件。 I also saw that you could use e.added.text to get the text of the option and e.added.id to get the id, which is similar to e.val mentioned previously.我还看到可以使用e.added.text获取选项的文本,使用e.added.text . e.added.id获取 id,这与e.val提到的e.val类似。

In case you need to test e.vale agains a condition, make sure to turn it into a string or numeric.如果您需要再次测试 e.vale 条件,请确保将其转换为字符串或数字。 Like this:像这样:

var newValue = (e.val).toString();

This way we can make sure that, if the value is empty (false) it will return false properly in a condition and if it has any value in it, it will return true as it should.通过这种方式,我们可以确保,如果值为空(false),它将在条件下正确返回 false,如果其中有任何值,它将按应有的方式返回 true。 The reason is that if I leave it as an object, it will always be true.原因是如果我把它作为一个对象,它永远是真的。 Also this method works flawlessly with multiple selected options, just by adding the parameter multiple="multiple" to the select tag.此外,此方法可以完美地处理多个选定的选项,只需将参数multiple="multiple"到选择标记即可。

This was tested (Surprisingly) with Select2 4.0.8, Select2 3.5.2 and Select2 3.5.3 and it ended up working.这是用 Select2 4.0.8、Select2 3.5.2 和 Select2 3.5.3 测试的(出人意料地),它最终正常工作。 Again I am pretty sure the other answers here worked, but now I had to resort to this trickery to get it to work (After 2 hours figuring out why it did not).我再次很确定这里的其他答案有效,但现在我不得不求助于这个诡计来让它工作(2小时后弄清楚为什么它没有)。

    $(document).ready(function() {
        //Init select2
        $('#id').select2();
        //Log selected value to console
        $('#id').on('change', function(e) {
            console.log($('#id').select2("val")); 
        });
    });

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

相关问题 加入一个select2的选中选项的文本,并设置一个输入框的值 - Join the text of the selected options of a select2 and set the value of an input box jQuery-在选择输入中获取选择的选项的值 - jquery - get the value of a selected option in a select input 如何设置 jQuery Select2 的选定值? - How to set selected value of jQuery Select2? 如何使用 JavaScript\\JQuery 使用选择标记的选定选项的值设置隐藏输入标记的值? - How can I set the value of an hidden input tag with the value of the selected option of a select tag using JavaScript\JQuery? 如何获取选择jquery对象的所选选项的文本值? - How to get the text value of the selected option of a select jquery object? 对于select2多个中的每个选项,请使用jquery将选项值文本附加到div中 - for each option from select2 multiple, append option value text into div using jquery jQuery - 使用值或文本以及嵌套在optgroups中的选项设置选择框的选定选项 - jQuery - Set the selected option of a select box using value or text with options nested in optgroups 如何在jQuery中设置选择选项的选定值? - How to set the selected value of Select Option in jQuery? JQuery select2从列表中的选项设置默认值? - JQuery select2 set default value from an option in list? 获取所选选项Select2的单个值 - Getting single value of selected option Select2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM