Jquery - Change value from option value

前端 未结 4 651
暖寄归人
暖寄归人 2021-01-26 14:27

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:05

    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)
  • 2021-01-26 15:12

    With jQuery:

    $("#ListSujet option").each(function(){
        $(this).val($(this).val().replace(",True", ""));
    }
    
    0 讨论(0)
  • 提交回复
    热议问题