0
votes

Supprimer un texte dans une liste déroulante avec jQuery

J'ai besoin de supprimer ce qui est écrit sur une liste d'outils, un problème grave, je ne trouve pas le code correct, cela fonctionne parfait s'il a une valeur mais dans mon cas, je dois supprimer ce qui est écrit sur la liste déroulante Liste

p>

<html lang="en">

<head>
  <meta charset="utf-8">
  <title>keypress demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>

<body>
  <select name="selectBox" id="selectBox">
    <option value="1">option1</option>
    <option value="2">option2</option>
    <option value="3">option3</option>
    <option value="4">option4</option>
  </select>
  <script>
    //it does not work
    //$("select[name^=selectBox]").option("[text='option1']").remove();
    //$("select[name^=selectBox] option[text='option1']").remove();

    //if it works but I need to delete what is written in the options
    $("select[name^=selectBox] option[value='1']").remove();
  </script>


2 commentaires

Vous devez définir une valeur des options


Vous souhaitez effacer une option ou supprimer une option?


3 Réponses :


1
votes

Vous pouvez utiliser un filtre :

p>

<html lang="en">

<head>
  <meta charset="utf-8">
  <title>keypress demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>

<body>
  <select name="selectBox" id="selectBox">
    <option value="1">option1</option>
    <option value="2">option2</option>
    <option value="3">option3</option>
    <option value="4">option4</option>
  </select>
  <script>
    $("#selectBox")                             // why not use the id selector - much more efficient than an attribute selector
        .children('option:contains("option1")') // get the options that contains option1
        .remove();                              // remove the filtered option
  </script>
</body>
</html>


0 commentaires

0
votes

Vous devez définir une valeur des options.

Créez comme suit: P>

$("select[name^=selectBox] option[value='1']").text("");
 $("select[name^=selectBox]").value("");


0 commentaires

0
votes

Définir un identifiant sur votre étiquette d'option:


0 commentaires