jquery 如何显示所选字段值的下一个字母?

s71maibg  于 2023-02-08  发布在  jQuery
关注(0)|答案(3)|浏览(97)

假设我有一个选择字段

$('#myselect').on('change' , function() {
  var a = $('#result'); 
  select = $(this).val();  // Get the value  
  selectValue=$(this).find(':selected').data("value");   
  a.find('.b').html(selectValue);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<select id="myselect" style="" name="select[]" class="select">
         <option data-value="ABC">My product</option>
         <option data-value="BCD">My second product</option>
   </select>

   <div id="result">
   <span class="b"></span>
   </div>

因此,结果将是"ABC"和"BCD",相反,我想显示"BCD"为第一个,"CDE"为第二个..因此,显示值的下一个字母在英语字母表..
任何帮助都将不胜感激...

zu0ti5jz

zu0ti5jz1#

一种方法如下,在JavaScript代码中带有解释性注解:

// we select the element with the id of 'myselect', and
// bind the anonymous function of the on() method as the
// event-handler for the 'change' event:
$('#myselect').on('change', function() {

  // we use the spread syntax, with an Array-literal, to
  // we use the CSS selector 'option:checked' to find the
  // <option> element that has been selected, retrieve its
  // value and then convert that iterable String into an
  // Array using the spread syntax with an Array-literal;
  // we then call Array.prototype.map() to create a new
  // Array based on the first:
  let result = [...$(this).find('option:checked').val()].map(
    // within the map() method, we use an anonymous Arrow
    // function to pass in the current letter of the String:
    (letter) => {
      // we use String.prototype.charCodeAt() to retrieve the
      // the char-code of the current letter (passing an index
      // of zero to do so, as it's the only letter and
      // JavaScript is zero-based):
      let characterCode = letter.charCodeAt(0),
          // to retrieve the char-code of the next letter
          // I assumed that we should wrap around to 'A'/'a'
          // if the current letter is 'Z'/'z':
          // we first find if the Array of numbers 90 and 122
          // (the char codes for 'Z' and 'z' respectively);
          // if so we subtract 26 from the character code (to
          // return the 'A' of the relevant case, or if not
          // we return the character-code + 1:
          nextCode = [90, 122].includes(letter) ? characterCode - 26 : characterCode + 1;
          
      // here we return the next letter through the use of
      // String.prototype.fromCharCode() and passing in the
      // nextCode variable:
      return String.fromCharCode(nextCode);
    // we then use Array.prototype.join() to create a String
    // from the Array of letters:
    }).join('');
    
  // selecting the element(s) with a class-name of 'b' that are
  // nested within the element with an id of 'result':
  $('#result .b').text(result)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<select id="myselect" name="select[]" class="select">
  <option value="ABC" data-value="ABC">My product</option>
  <option value="BCD" data-value="BCD">My second product</option>
</select>

<div id="result">
  <span class="b"></span>
</div>

JS Fiddle demo.
当然,这在不使用库的纯JavaScript中是完全可能的,如下所示:
一个二个一个一个
JS Fiddle demo.
当然,使用change确实需要用户选择一个不同于起始值的值(假设他们希望起始值是他们的选择),所以我建议调整HTML:

$('#myselect').on('change', function() {
  let result = [...$(this).find('option:checked').val()].map(
    (letter) => {
      let characterCode = letter.charCodeAt(0),
          nextCode = [90, 122].includes(letter) ? characterCode - 26 : characterCode + 1;
          
      return String.fromCharCode(nextCode);
    }).join('');
    
  $('#result .b').text(result)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<select id="myselect" name="select[]" class="select">
  <!-- here we use an <option> element that is both selected, so that it's shown on
       page-load, and disabled so that it can't be selected by the user once the
       select has been opened; forcing them to choose one of the other options: -->
  <option selected disabled>Please select:</option>
  <option value="ABC" data-value="ABC">My product</option>
  <option value="BCD" data-value="BCD">My second product</option>
</select>

<div id="result">
  <span class="b"></span>
</div>

JS Fiddle demo.
参考文献:

7cwmlq89

7cwmlq892#

我不知道我是否正确地理解了您的请求,但下面的代码,从我所理解的当然,应该能够帮助您。

$(function(){

$('#myselect').on('change' , function() {

    let nbLetter    = 3;
    let beginCode   = $(this).find(':selected').data("value").charCodeAt(0);
    let myValue     = "";

    // Construct New Value
    for(let i=0;i<nbLetter;i++) myValue += String.fromCharCode(beginCode+1+i);

    console.log(myValue);
});

});
2ekbmq32

2ekbmq323#

我不知道你的意思是得到下一个元素的值还是什么,如果我理解正确,这段代码将为您工作

$(document).ready(function() {
      $("#myselect").change(function() {
        var selectedOption = $(this).find("option:selected");
        var nextOption = selectedOption.next();
    var a = $('#result');
    a.find('.b').html(nextOption.data("value"));

      });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <select id="myselect" style="" name="select[]" class="select">
             <option data-value="ABC">My product</option>
             <option data-value="BCD">My second product</option>
             <option data-value="CDE">My Third product</option>
       </select>

       <div id="result">
       <span class="b"></span>
       </div>

相关问题