Results 1 to 6 of 6

Thread: A Combination of 4 Check Boxes

  1. #1
    Join Date
    Nov 2007
    Location
    Stilton
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A Combination of 4 Check Boxes

    Hello to all you lovely programmers,who I hope will help me

    I wish to set up a page with 4 check boxes so that you can select a combination of all 4 boxes A, B, C, D.
    (ie) By selecting A you will go to a page title A
    By selecting A,B you will go to a page title AB and so on
    is this possible with javascript as I have been looking for a program for a few days now with no success

    Thank PiperJim

  2. #2
    Join Date
    Nov 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    when you want to submit it you need to have the action value as variabel

    <input type="hidden" id="hidden_id" value="">
    <form id="form_id" method="post" action="<scritp>document.getElementById('hidden_id').value</script>">

    ..........
    ........
    <input type="checkbox" id="c1">
    <input type="checkbox" id="c2">
    <input type="checkbox" id="c3">
    <input type="checkbox" id="c4">

    -----

    <script>
    var done =0;
    if(document.getElementById("c1").checked){
    done++}
    else if(document.getElementById("c2").checked){
    done++
    }
    .....
    ....
    ...
    // now you need to change submit

    function submit(){
    if(done==1){
    document.getElementById("hidden_id").value="http://dlk//";
    form_id.submit();
    </script>

    <input type="button" onclick="submit()">

  3. #3
    Join Date
    Nov 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    function submit should be like this


    function submit(){
    if(done==1){
    document.getElementById("hidden_id").value="http://dlk//";
    form_id.submit();
    }

    else if(done==2){
    document.getElementById("hidden_id").value="http://2.htm";
    form_id.submit();
    }
    }

  4. #4
    Join Date
    Nov 2007
    Location
    Stilton
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you for everything I have now got a program up and running Thanks again
    Piperjim

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <script type="text/javascript">
      function getValue(form, name) {
        for(var i = 0, f = form.elements[name], n = f.length, r = []; i < n; ++i)
          f[i].checked && r.push(f[i].value);
        return r.length ? r : null;
      }
    </script>
    
    <form action="redirector.php" onsubmit="this.action = (getValue(this, 'page') || ['error']).join('');">
      <input type="checkbox" name="page" value="A">
      <input type="checkbox" name="page" value="B">
      <input type="checkbox" name="page" value="C">
      <input type="checkbox" name="page" value="D">
    </form>
    ... where "redirector.php" is a page that will do the same thing for non-JS users.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    @Twey

    Interesting idiom using
    Code:
    predicate && statement;
    vs
    Code:
    if(predicate) statement;
    Code:
    f[i].checked && r.push(f[i].value);
    Do you do it for reasons other than simply being fancy?

    Does it also work in other languages, like C++ or Java?
    Trinithis

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
  •