Jquery - Change value from option value

前端 未结 4 804
Happy的楠姐
Happy的楠姐 2021-01-26 14:35

I already have this :


                        
    
提交评论

  • 2021-01-26 14:59

    $.each($("select").find("option"), function() {
      var splitVal = $(this).val().split(",")
      $(this).val(splitVal[0])
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <select id="ListSujet" name="ListSujet">
    <option value="514402,True">Copie de Musculation</option>
    <option value="514430,True">Gecko Leopard</option>
    </select>

    0 讨论(0)
  • 2021-01-26 15:01

    Try this:

    $("#ListSujet option").each(function(){
      $(this).val($(this).val().split(",")[0]);
      //console.log($(this).val());
    });
    
    $("#ListSujet").on("change",function(){
      console.log($(this).val());
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <select id="ListSujet" name="ListSujet">
    <option value="514402,True">Copie de Musculation</option>
    <option value="514430,True">Gecko Leopard</option>
    </select>

    0 讨论(0)
  • 2021-01-26 15:07

    You can use callback function of .attr() to modify existing value:

    $('#ListSujet option').attr('value',function(i,o){
        return  o.substring(0, o.indexOf(','))
    });
    

    Working Snippet:

    $('#ListSujet option').attr('value',function(i,o){
       return  o.substring(0, o.indexOf(','))
    });
    
    $("#ListSujet").on("change",function(){
      console.log($(this).val());
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <select id="ListSujet" name="ListSujet">
    <option value="514402,True">Copie de Musculation</option>
    <option value="514430,True">Gecko Leopard</option>
    </select>

    0 讨论(0)
  • 提交回复
    热议问题