SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: jQuery in Forms

  1. #1
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery in Forms

    I have a set of radio buttons in a form with the last option being other. I would like to disable (or hide) a textfield next to other unless if it is selected. I would also like the textfield to be required if other is selected. It shouldn't be required if other is not selected. And finally, I would like it if when the form is submitted and other is selected, instead of other being posted, the textfield information is inserted into the form. I am currently using jQuery on this page already for another form option. Please help.

    I asked a similar question that was answered in this thread: http://www.sitepoint.com/forums/java...ml#post4857055. I didn't quite ask the question right though.

    Thanks in advance.

  2. #2
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this should do...
    HTML Code:
    <script>
    
    $(document).ready(function() {
        $('#myForm').submit(function() {
            if ($('input:radio[name=required]:checked').val() == 1) {
                return $('#myText').val() != '';
            }
        });
        $('input:radio[name=required]').change(function() {
            if ($('input:radio[name=required]:checked').val() == 1) {
                $('#myText').show();
            } else {
                $('#myText').hide();
            }
        });
    });
    </script>
    
    <form id="myForm" action="/" method="post">
    
    <input type="radio" name="required" value="1" />
    <input type="radio" name="required" value"0" />
    
    <input type="text" id="myText" name="input" value="" />
    </form>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •