Change the selected of an option that is in copy script

0

I'm new to the area and I'm having a lot of trouble doing the following:

  • I made a javascript script to create multiple copies of my option . So far so good: copies made successfully.
  • I need to make a for in javascript to go through each option copied and display a different phrase for each option in the first line.
  • I thought the following:

    • for that runs option
    • Finding the phrase x in a option any
    • Found the phrase x in option : add selected , so that the phrase appears as the first choice of option .

    Below is a test html code with the copy function, for better understanding:

    <!-- ... <html>, <head>, jQuery, etc ... -->
    <table cellpadding="10" cellspacing="10" border="1">
        <tr>
            <td>Lixo01</td>
            <td class="Original" id="Linha1">
                <select id="MaxAlarme3">
                    <option value="02">Teste1</option>
                    <option value="03">Teste2</option>
                    <option value="0">Teste3</option>
                    <option value="04">Teste4</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Lixo02 teste</td>
            <td class="Clonar" id="Linha2"></td>
        </tr>
        <tr>
            <td>Lixo03 value 05</td>
            <td class="Clonar" id="Linha3"></td>
        </tr>
        <tr>
            <td>Lixo04</td>
            <td class="Clonar" id="Linha4"></td>
        </tr>
        <tr>
            <td>Lixo05</td>
            <td class="Clonar" id="Linha5"></td>
        </tr>
        <tr>
            <td>Lixo06</td>
            <td class="Clonar" id="Linha6"></td>
        </tr>
    </table>
    <script>
        //variavel contar o tamanho das copias
        $(document).ready(function() {
            //Copia valor da Linha1.Classe
            var Copiar = $("#Linha1.Original").html();
            //Clona o valor da Linha 1 para a Classe Clonar
            $(".Clonar").append(Copiar);
        });
    </script>
    
        
    asked by anonymous 27.02.2014 / 12:40

    3 answers

    0

    I got what I wanted to eeee.

    // frase que desejo localizar
        var frase      = "04";//eu queria que a variavel frase pegasse o value dos options e de acordo com o valor do
        //option eu poderia escolher a frase que iria iniciar o mesmo
           var localizado = null;
    
        // loop que percorre cada uma das opções
        // e verifica se a frase da opção confere com o
        // valor de fase que está sendo procurado
        $('#Linha6 option').each(function() {
          // se localizar a frase, define o atributo selected
          if($(this).attr('value') == frase) {//-----Alterei de $(this).value() para o attr.()-----
            $(this).prop('selected', true); //-----Pode-se utilizar .attr('selected', true) também-----
          }
        });
    

    Thank you all for the help.

        
    28.02.2014 / 14:44
    1

    HTML:

    <select id="opcoes">
        <option>Selecione</option>
        <option>Lorem ipsum Cupidatat sunt exercitation fugiat Excepteur.</option>
        <option>Lorem ipsum Elit sint velit nostrud minim quis.</option>
        <option>Lorem ipsum Id cillum est dolor.</option>
        <option>Lorem ipsum Irure Ut veniam cupidatat dolor culpa.</option>
        <option>Lorem ipsum Pariatur et Duis cupidatat.</option>
        <option>Lorem ipsum Esse laboris veniam irure eu irure.</option>
        <option>Lorem ipsum Anim eu culpa magna quis irure.</option>
        <option>Lorem ipsum Dolore Excepteur esse aliquip Duis eu.</option>
        <option>Lorem ipsum Duis ut velit non.</option>
    </select>
    

    JavaScript:

    $(document).ready(function() {
        // frase que desejo localizar
        var frase      = "Lorem ipsum Dolore Excepteur esse aliquip Duis eu.",
            localizado = null;
    
        // loop que percorre cada uma das opções
        // e verifica se a frase da opção confere com o
        // valor de fase que está sendo procurado
        $('#opcoes option').each(function() {
          // se localizar a frase, define o atributo selected
          if($(this).text() == frase) {
            $(this).attr('selected', true);
          }
        });
    });
    
        
    27.02.2014 / 13:01
    0

    Well, one way to keep your code cleaner, would be to use the $ .find method as below:

    HTML

    <select id="opcoes">
      <option value="0">Selecione</option>
      <option value="1">Lorem ipsum Cupidatat sunt exercitation fugiat Excepteur.</option>
      <option value="2">Lorem ipsum Elit sint velit nostrud minim quis.</option>
      <option value="3">Lorem ipsum Id cillum est dolor.</option>
      <option value="4">Lorem ipsum Irure Ut veniam cupidatat dolor culpa.</option>
      <option value="5">Lorem ipsum Pariatur et Duis cupidatat.</option>
      <option value="6">Lorem ipsum Esse laboris veniam irure eu irure.</option>
      <option value="7">Lorem ipsum Anim eu culpa magna quis irure.</option>
      <option value="8">Lorem ipsum Dolore Excepteur esse aliquip Duis eu.</option>
      <option value="9">Lorem ipsum Duis ut velit non.</option>
    </select>
    

    JavaScript

    $(document).ready(function() {
      // valor que desejo localizar
      var value = "1";
    
      // Usa-se o método $.find para buscar a
      // opção pelo atributo value e o seleciona
      $('#opcoes').find('[value="' + value + '"]').attr('selected', true);
    });
    
        
    05.03.2014 / 12:18