简体   繁体   中英

Add new elements to a javascript object dynamically

I'm trying to do a page with HTML and JQuery. What I want to do with this is typing an e-mail in a textbox and when a button is pressed, add him to an object, and show the new e-mail added and the ones already sent. My problem is that the new e-mail replace the one I had, It's not added after it. How can I solve it?

Here's my code

enter code here

<html>
<head>
    <script src="config/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" language="javascript">
        var obj = {};

        $(document).ready(function(){
            $('#agr').click(function(){
                var correo_val = $('#correoe').val();
                var estrc = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

                if(correo_val == ''){
                    alert("Debes agregar un correo");
                }
                else if(!estrc.test(correo_val)){
                    alert("Correo no valido");
                }
                else if(estrc.test(correo_val)){
                    alert("Correo agregado exitosamente");
                    if(!obj.hasOwnProperty(correo_val)){
                        arr = [];
                        arr.push(correo_val);
                        // obj=arr;
                        obj2 = {};
                        obj2 = arr;
                        obj = obj2;

                    }else{  

                        obj2 = {};
                        obj = obj2;
                    }

                    alert(obj);
                    $("#listac").html(obj);
                }
            });
        });

    </script>
</head>

<body>
    <div>
        <table style='margin-top:10%' align='center'>
            <tr> <td> Ingresar correo electr&oacutenico                    </td> </tr>
            <tr> <td> <input type='text' name='correoe' id='correoe'>                  </td> </tr>
            <tr> <td> <input type='button' name='agregar' value='agregar' id='agr'> </td> </tr>
        </table>
    </div>

    <div style="background:#FFFFFF;border:0" class="box" id="LCriterios">
        <h1><span id="field"></span><span style="float:right;"></span></h1>
        <!--<label><span>Tipo de operaci&oacuten</b></span><select class='opSelect' name='selOp' id='selOp'></select></label>
        <label id='selCriterio'></label>
        <label style="height: 30px;"><input type="button" class="btAgregar" value="Agregar" id="btn_add"/></label>-->
        <label id="label12">Correos electr&oacutenicos </label>
        <div id='listac'></div>
    </div>

    </body>

I've just made a Fiddle with one adjustment of your javascript -

if(!obj.hasOwnProperty(correo_val)){
                    arr = []; //  <-- move this to the first line where
                              // you already have var obj = {};
                    arr.push(correo_val);
                    ...

Move the arr = []; that you use to store the valid emails from this if() clause to the first line of your code where you also declared var obj = {}; . Otherwise arr = []; will be an empty array every time before you add another email and always have only one element (the last email that was added).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM