jquery
  • checkbox
  • 2011-04-03 81 views 0 likes 
    0
    function sendValues() { 
    var str = $("#ryan_m").serialize(); 
    var response = $('input[name=brand[]]:checked').val(); 
    $.ajax({ 
        url: "/trying1.php?avoidcache=' + myTimestamp();", 
        data: {str} 
        cache: false 
    }); 
    } 
    
    <form action="trying1.php?b_id=$brand_id" method="get"/></td> 
        <input type="checkbox" name="brand[]" value="$brand_id"/> 
         <input type="submit" value="Compare" id="ryan_m" method="get" /> 
          </form> 
    

    我想在檢查可用的複選框時使用此Ajax請求執行。 目前,這個腳本將完成我想要的,但我必須點擊提交才能執行。使用jquery選擇器(複選框)執行onClick

    我的問題:

    有沒有我可以在點擊複選框執行AJAX,沒有擊中提交一份簡單的方法?

    +0

    你的問題解決了。請選擇一個工作答案作爲解決方案。 – buschtoens 2011-04-03 20:41:32

    回答

    0

    您可以使用jQuery .submit()提交關於單擊窗體。

    <form action="trying1.php?b_id=$brand_id" id="myForm" method="get"/></td> 
        <input type="checkbox" name="brand[]" class="myCheckbox" value="$brand_id"/> 
        <input type="submit" value="Compare" id="ryan_m" method="get" /> 
    </form> 
    

    給你表格一個ID,給複選框一個類,或者如果你想要一個ID,如果只有一個。然後在你的jQuery中。

    function sendValues() { 
        var str = $("#ryan_m").serialize(); 
        var response = $('input[name=brand[]]:checked').val(); 
        $.ajax({ 
         url: "/trying1.php?avoidcache=' + myTimestamp();", 
         data: {str} 
         cache: false 
        }); 
    } 
    
    $('.myCheckbox').change(function(){ 
        $('#myForm').submit(function(){ 
         sendValues(); 
         return false; 
        }); 
    }); 
    

    我還沒有測試過代碼,但多數民衆贊成你的想法。

    +0

    爲什麼要發射不必要的事件? – buschtoens 2011-04-03 20:35:32

    +0

    這是行不通的,事實上,它沒有點擊提交按鈕就不會調用頁面,這是我不想做的。 – 2011-04-03 20:36:42

    1
    $("input[type=checkbox]").change(function() { 
        $("form").submit(); 
    }); 
    
    +0

    什麼是目標? ;) 如果該複選框被(鍵盤)打勾,該怎麼辦? – buschtoens 2011-04-03 20:27:27

    +0

    是的,這不起作用,但謝謝你。 – 2011-04-03 20:31:27

    +0

    目標是由用戶填寫。他沒有添加一個類到他的複選框,所以我只是把目標。無論如何RPM我編輯代碼以使用類型複選框的輸入。你可以試試。另外.click可能需要.change。 – 2011-04-03 20:34:00

    0
    $("input[name=brand[]]").change(function() { 
        sendValues(); 
        alert("Data is sent."); // Remove this - Just for testing purpose 
    }); 
    

    jQuery Change Event。 這可以檢測各種<input>的各種變化。 <select><textarea>

    0
    $(document).ready(function() { 
        $('[name="brand[]"]').change(function() { 
         sendValues(); 
        }); 
    }); 
    

    演示在這裏:http://jsfiddle.net/JtBe2/

    +0

    你只是在檢測點擊。不要通過鍵盤或其他腳本進行更改。 – buschtoens 2011-04-03 20:29:56

    +0

    謝謝你提到它,修正。 – Peeter 2011-04-03 20:30:54

    相關問題