Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2010
    Messaggi
    23

    problema con checkbox e relativa funzione

    ciao a tutti.
    Vorrei realizzare una cosa che sembrerebbe semplicissima (benché non riesca a farla funzionare!): semplicemente vorrei che accanto ad un form di ricerca ci siano più input button ed in più due input radio con cui è possibile scegliere di aprire il link collegato a ciascun pulsante nella stessa o in un'altra finestra.
    Tuttavia non riesco a configurare bene lo script in javascript per fare questo evitando di ripetere per ogni singolo pulsante le varie istruzioni...
    Per spiegarmi meglio vi faccio vedere uno script molto spartano che ho scritto e che pensavo potesse risolvere questo problema ma non so come mai non funziona:
    codice:
    <form name="test"> <input type="text" name="query" value=""/> <input type="button" value="cerca" onclick="search("google");"/> <input type="button" value="cerca con googleimmagini" onclick="search("googleimmagini");"/> <input type="radio" name="checkgroup" value="stessa scheda" checked="checked" /> <input type="radio" name="checkgroup" value="nuova scheda" /> </form>  
    <script type="text/javascript"> function search(a){  
    var link;  
    len = document.test.checkgroup.length; 
    for (i=0; i<len; i++) { if (document.test.checkgroup[0].checked){         window.open(link); } else {  document.location.href=link;         
    } 
    if (a === "google") 
    {    link = "http://www.google.it/search?q=" + document.test.query.value; 
    } else if (a==="googleimmagini") {                 
    link = "http://images.google.it/search?tbm=isch&hl=it&source=hp&biw=1920&bih=859&q=" + document.test.query.value; }  } }  
    </script>
    Cosa sbaglio? Esiste qualche altro metodo più sintetico? magari con un array in cui inserisco i vari link? grazie a tutti

  2. #2
    Ad una prima occhiata mi sembra ok.
    Solo che:
    1) La variabile link la inizializzerei prima di fare la window.open
    2) Non ho capito a che ti serve

    codice:
    len = document.test.checkgroup.length;
    for (i=0; i<len; i++) {...}
    Hai due radio, controlla semplicemente se la name[0] è checked, altrimenti lo sara' sicuramente name[1]

    Sono le 8:00 e ho sonno, prendi tutto con le molle

  3. #3

    Re: problema con checkbox e relativa funzione

    codice:
    <form name="test"> 
    <input type="text" name="query" value=""/> 
    <input type="button" value="cerca" onclick="search("google");"/> 
    <input type="button" value="cerca con googleimmagini" onclick="search("googleimmagini");"/> 
    <input type="radio" name="checkgroup" value="stessa scheda" checked="checked" id="samePage"  /> 
    <input type="radio" name="checkgroup" value="nuova scheda" /> 
    </form>  
    <script type="text/javascript"> 
    function search(a){
    var link;
    if(a=="google"){
    link = "https://www.google.it/search?q=" + document.test.query.value;
    }else{
    link = "http://images.google.it/search?tbm=isch&hl=it&source=hp&biw=1920&bih=859&q=" + document.test.query.value;
    }
    if(document.getElementById('samePage').checked){
    document.location.href=link;   
    }else{
    window.open(link); 
    }  
    </script>

  4. #4
    Utente di HTML.it
    Registrato dal
    Oct 2010
    Messaggi
    23

    Re: Re: problema con checkbox e relativa funzione

    Originariamente inviato da lucavizzi
    codice:
    <form name="test"> 
    <input type="text" name="query" value=""/> 
    <input type="button" value="cerca" onclick="search("google");"/> 
    <input type="button" value="cerca con googleimmagini" onclick="search("googleimmagini");"/> 
    <input type="radio" name="checkgroup" value="stessa scheda" checked="checked" id="samePage"  /> 
    <input type="radio" name="checkgroup" value="nuova scheda" /> 
    </form>  
    <script type="text/javascript"> 
    function search(a){
    var link;
    if(a=="google"){
    link = "https://www.google.it/search?q=" + document.test.query.value;
    }else{
    link = "http://images.google.it/search?tbm=isch&hl=it&source=hp&biw=1920&bih=859&q=" + document.test.query.value;
    }
    if(document.getElementById('samePage').checked){
    document.location.href=link;   
    }else{
    window.open(link); 
    }  
    </script>
    ti ringrazio per i suggerimenti ma purtroppo continua a non funzionare..non riesco davvero a capire cosa ci sia di sbagliato

  5. #5
    Utente di HTML.it
    Registrato dal
    Oct 2010
    Messaggi
    23

    Re: Re: Re: problema con checkbox e relativa funzione

    Originariamente inviato da calcas2
    ti ringrazio per i suggerimenti ma purtroppo continua a non funzionare..non riesco davvero a capire cosa ci sia di sbagliato
    ps: nel codice che tu mi hai suggerito manca alla fine una parentesi graffa ma non è quello l'errore nel senso che comunque non funziona lo stesso

  6. #6
    "search" è una parola riservata di javascript, cambia il nome della funzione.

  7. #7
    Utente di HTML.it L'avatar di vic53
    Registrato dal
    Oct 2010
    residenza
    Fonte Nuova (Roma)
    Messaggi
    592

    ciao ti posto il codice completo

    prova questo codice che ho scritto in una mezzoretta e ho provato...puo farti da guida...
    ciao
    Vic53
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>Ricerca sui motori web</title>
    <script type="text/javascript" language="javascript">

    function TestCheckboxes() {
    var URL = "";
    qText = document.Test.query.value;
    for (i=0 ;i<4 ;i++) {
    if (document.Test.Radio1[i].checked) {
    URL = document.Test.Radio1[i].value;
    }
    }
    URL = URL + qText;
    if (document.Test.CK1[0].checked) {
    // alert("Same page search...");
    StartPage(URL);
    }
    else {
    if (document.Test.CK1[1].checked) {
    // alert("in a new window search...");
    popWND(URL, 800, 600);
    }
    }
    }
    // apre una window e gli passa URL, larghezza e altezza
    function popWND(URL, x, y) {
    var goPopX = (screen.width - x) / 2;
    var goPopY = (screen.height - y) / 2;
    var loc = "left=" + goPopX + ",top=" + goPopY;
    goPopWindow = window.open(URL, "popWindow", "scrollbars,menubar=no,location=no,resizable=yes,h istory=no,titlebar=no,width=" + x + ",height=" + y + "," + loc);
    }
    function StartPage(URL) {
    alert(URL);
    document.location.href = URL;
    }
    </script>
    <style type="text/css">
    .style1
    {
    width: 109%;
    }
    .style2
    {
    width: 238px;
    text-align: center;
    }
    .style3
    {
    width: 38px;
    }
    .style4
    {
    color: #0000FF;
    }
    .style5
    {
    width: 30px;
    }
    .style7
    {
    color: #0000FF;
    font-weight: bold;
    }
    .style8
    {
    font-family: Verdana;
    font-size: medium;
    color: #333399;
    }
    </style>
    </head>
    <body>
    <form action="" method="post" name="Test" id="Test" >
    <table class="style1">
    <tr>
    <td colspan="3" class="style8">
    Ricerca sui principali motori Internet</td>

    </tr>
    <tr>
    <td class="style2">
    <table class="style1">
    <tr>
    <td class="style3">
    <input type="radio" name="CK1" id="CK1" value="0" checked="checked"
    class="style4" /></td>
    <td class="style4">
    in the same page...</td>
    </tr>
    <tr>
    <td class="style3">
    <input type="radio" name="CK1" id="CK1" value="1" /></td>
    <td class="style4">
    in a new window...</td>
    </tr>
    </table>
    </td>
    <td colspan="2">
    <table class="style1">
    <tr>
    <td class="style5">
    <input id="Radio1" name="Radio1" type="radio" checked="checked" value="http://www.google.it/#hl=it&q=" /></td>
    <td class="style4">
    Google</td>
    </tr>
    <tr>
    <td class="style5">
    <input id="Radio1" name="Radio1" type="radio" value="http://it.bing.com/search?q=" /></td>
    <td class="style7">
    MSN</td>
    </tr>
    <tr>
    <td class="style5">
    <input id="Radio1" name="Radio1" type="radio" value="http://ricerca.virgilio.it/ricerca?f=hp&qs="/></td>
    <td class="style7">
    Virgilio</td>
    </tr>
    <tr>
    <td class="style5">
    <input id="Radio1" name="Radio1" type="radio" value="http://www.altavista.com/search;_ylt=A0oG7pbGN5RPuD0A5n2l87UF?p=" /></td>
    <td class="style7">
    Altavista</td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td class="style2">
    <input type="text" name="query" value="" style="width: 285px"/></td>
    <td>
    <input type="button" value="Start search..." onclick="return TestCheckboxes();"
    style="width: 163px; font-weight: 700; color: #FF0000;"/></td>
    <td>
    </td>
    </tr>
    <tr>
    <td class="style2">
    </td>
    <td>
    </td>
    <td>
    </td>
    </tr>
    <tr>
    <td class="style2">
    </td>
    <td>
    </td>
    <td>
    </td>
    </tr>
    </table>

    </form>

    </body>
    </html>
    Vic53

  8. #8
    Utente di HTML.it
    Registrato dal
    Oct 2010
    Messaggi
    23
    grazie a tutti ma non riesco proprio a capire che cosa è che non va in questa funzione....davvero non ci riesco...qualcuno sa darmi qualche spiegazione? grazei mille..
    Altro tentativo infruttuoso che ho fatto è stato questo:

    codice:
    <form name="test">  
    <input type="text" name="query" value=""/>  
    <input type="button" value="cerca" onclick="variante(sName[0]);"/>  
    <input type="button" value="cerca con googleimmagini" onclick="variante(sName[1]);"/>  <input type="radio" name="checkgroup" value="stessa scheda" checked="checked" id="samePage"  />  
    <input type="radio" name="checkgroup" value="nuova scheda" />  
    </form>   
    <script type="text/javascript">  function variante(a){ 
    var sName = []; 
    sName[0]= "http://www.google.it/search?q=" + document.test.query.value; 
    sName[1]= "http://images.google.it/search?tbm=isch&hl=it&source=hp&biw=1920&bih=859&q=" + document.test.query.value; 
    for (i=0; i<2; i++) { 
    if(a===sName[i] && document.test.checkgroup[0].checked){ document.location.href=sName[i]; 
    }else if (a===sName[i] && document.test.checkgroup[1].checked) { window.open(sName[i]); } 
    } 
    }  </script>
    Dove sbaglio?

  9. #9
    Utente di HTML.it L'avatar di vic53
    Registrato dal
    Oct 2010
    residenza
    Fonte Nuova (Roma)
    Messaggi
    592
    Ti ho corretto il tuo codice
    e l'ho provato....
    ciao
    vic53
    ----------
    <html>
    <head>
    <title>Sans Titre</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="generator" content="HAPedit 3.1">
    <script type="text/javascript">
    function variante(a){
    var sName = [];
    sName[0]= "http://www.google.it/search?q=" + document.test.query.value;
    sName[1]= "http://images.google.it/search?tbm=isch&hl=it&source=hp&biw=1920&bih=859&q =" + document.test.query.value;
    for (i=0; i<2; i++) {
    if (a==i) {
    if (document.test.checkgroup[0].checked)
    document.location.href=sName[i];
    else
    window.open(sName[i]);
    }
    }
    }

    </script>
    </head>
    <body bgcolor="#FFFFFF">

    <hr />

    <form action="" method="post" name="test" id="Test">
    <input type="text" name="query" value=""/>
    <input type="button" value="cerca" onclick="variante(0);"/>
    <input type="button" value="cerca con googleimmagini" onclick="variante(1);"/>
    <input type="radio" name="checkgroup" value="0" checked="checked" />
    <input type="radio" name="checkgroup" value="1" />
    </form>


    </body>
    </html>
    Vic53

  10. #10
    Utente di HTML.it
    Registrato dal
    Oct 2010
    Messaggi
    23
    Originariamente inviato da vic53
    Ti ho corretto il tuo codice
    e l'ho provato....
    ciao
    vic53
    ----------
    <html>
    <head>
    <title>Sans Titre</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="generator" content="HAPedit 3.1">
    <script type="text/javascript">
    function variante(a){
    var sName = [];
    sName[0]= "http://www.google.it/search?q=" + document.test.query.value;
    sName[1]= "http://images.google.it/search?tbm=isch&hl=it&source=hp&biw=1920&bih=859&q =" + document.test.query.value;
    for (i=0; i<2; i++) {
    if (a==i) {
    if (document.test.checkgroup[0].checked)
    document.location.href=sName[i];
    else
    window.open(sName[i]);
    }
    }
    }

    </script>
    </head>
    <body bgcolor="#FFFFFF">

    <hr />

    <form action="" method="post" name="test" id="Test">
    <input type="text" name="query" value=""/>
    <input type="button" value="cerca" onclick="variante(0);"/>
    <input type="button" value="cerca con googleimmagini" onclick="variante(1);"/>
    <input type="radio" name="checkgroup" value="0" checked="checked" />
    <input type="radio" name="checkgroup" value="1" />
    </form>


    </body>
    </html>
    Grazie mille! sei stato gentilissimo e soprattutto mi hai fatto capire l'errore nel mio codice, grazie!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.