Welcome Guest, Not a member yet? Register   Sign In
JQuery with forms help needed
#10

[eluser]slowgary[/eluser]
Sure, you could do the array thing to. Then you'd just get rid of the iterator altogether. The reason the "Confusedelected" modifier didn't work is because it actually needs a space before it. I cleaned up the demo a bit, added some styles, used prepend instead of append so that new additions appear at the top, plus I made the submit button appear dynamically so that the form can only be submitted after a field is added. Try this:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html &gt;

&lt;head&gt;
    &lt;title&gt;&lt;/title>
    &lt;meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1' /&gt;
    &lt;style type='text/css'&gt;
        body {
            font-family: sans-serif;
        }

        .cause {
            position: relative;
            width: 200px;
            padding: 10px;
            margin: 5px 0px;
            background: #EEE;
            border: 1px solid #DDD;
        }

        select {
            padding: .15em;
            font-size: medium;
            margin-bottom: 20px;
        }

        #submit {
            width: 200px;
            padding: 10px;
            background: #EEE;
            border-top: 1px solid #CCC;
        }

        button {
            cursor: pointer;
            color: #FFF;
            padding: .2em .3em;
            line-height: 1em;
            font-size: medium;
            font-weight: bold;
            background: #59983B;
            border: 1px outset #59983B;
        }

        #submit input {
            cursor: pointer;
            color: #FFF;
            padding: 5px 10px;
            font-size: small;
            font-weight: bold;
            background: #3B5998;
            border: 1px outset #3B5998;
        }

        .remove {
            position: absolute;
            top: 50%;
            right: 15px;
            cursor: pointer;
            padding: 2px 4px;
            margin-top: -.8em;
            color: #999;
            font-size: 11px;
        }

        .remove:hover {
            color: #F00;
            background: #FCC;
        }
    &lt;/style&gt;
    &lt; script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js' &gt;&lt; /script >
    &lt; script type='text/javascript' &gt;
        //this code assumes jquery inclusion

        //assures the DOM is loaded before running this code
        $(document).ready(function(){

             //binds the following code to the 'click' of our button
             $('#add_cause').bind('click', function(){

                  if($('.cause').length == 0)
                  {
                       $('#causes').append("<div id='submit'>&lt;input type='submit' value='Add Record'/&gt;&lt;/div>");
                  }

                  //add the form field, plus a remove link, plus a container div
                  $('#causes').prepend(
                       "<div class='cause'>&lt;input type='hidden' name='causes[]' value='" +
                       $('#cause_select').val() +
                       "'/&gt;&lt;span>" +
                       $('#cause_select :selected').text() +
                       "</span><span class='remove'>[remove]</span></div>"
                  );
             });

             //bind the following code to all current and future elements with class 'remove'
             $('.remove').live('click', function(){
                  //look for a parent element with class 'cause' and remove it from the DOM
                  $(this).parents('.cause').remove();

                  if($('.cause').length == 0)
                  {
                       $('#submit').remove();
                  }
             });

        });
    &lt;/ script &gt;
&lt;/head&gt;

&lt;body&gt;
<select id='cause_select' name='cause_select'>
     <option value='first'>first cause</option>
     <option value='second'>second cause</option>
     <option value='third'>third cause</option>
     <option value='fourth'>fourth cause</option>
</select>
<button id='add_cause'>+</button>
&lt;form action='somescript' method='post'&gt;
     <div id='causes'>
     </div>
&lt;/form&gt;
&lt;/body&gt;

&lt;/html&gt;


Messages In This Thread
JQuery with forms help needed - by El Forum - 07-09-2009, 02:44 PM
JQuery with forms help needed - by El Forum - 07-09-2009, 10:15 PM
JQuery with forms help needed - by El Forum - 07-09-2009, 11:23 PM
JQuery with forms help needed - by El Forum - 07-10-2009, 01:57 AM
JQuery with forms help needed - by El Forum - 07-10-2009, 08:22 AM
JQuery with forms help needed - by El Forum - 07-10-2009, 11:10 AM
JQuery with forms help needed - by El Forum - 07-10-2009, 11:23 AM
JQuery with forms help needed - by El Forum - 07-10-2009, 11:39 AM
JQuery with forms help needed - by El Forum - 07-10-2009, 12:28 PM
JQuery with forms help needed - by El Forum - 07-10-2009, 01:16 PM
JQuery with forms help needed - by El Forum - 07-10-2009, 01:25 PM
JQuery with forms help needed - by El Forum - 07-10-2009, 01:38 PM



Theme © iAndrew 2016 - Forum software by © MyBB