2012-02-16 21 views
0

我只是發現jquery-mobile,但我發現文檔不適合「初學者」。如何使用事件?

所以,我有一些單選按鈕的fieldset。當用戶「檢查」一個按鈕時,如何做到將值保存在帶有ajax調用的php會話中?

<fieldset data-role="controlgroup"> 
    <legend>Choose a pet:</legend> 
    <input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked" /> 
    <label for="radio-choice-1">Cat</label> 

    <input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2" /> 
    <label for="radio-choice-2">Dog</label> 

    <input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3" /> 
    <label for="radio-choice-3">Hamster</label> 

    <input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4" /> 
    <label for="radio-choice-4">Lizard</label> 
</fieldset> 

回答

1

讓我們從一個jQuery例子開始簡單一點,您對jQuery的熟悉程度如何?

您需要將處理程序綁定到單選按鈕上的更改事件,然後在您設置會話變量的地方向AJAX調用PHP。

要綁定一個處理器在$(document).ready...

$('input[name="radio-choice-1"]').live('change', function(){ 
    var value = $(this).val(); 

    $.ajax({ 
     //see jQuery ajax functions 
    }); 
}); 



但在jQuery Mobile的看到jQuery的文檔的onlive功能,像這樣:

你應該使用jQM(jQuery Mobile)的pageinit事件運行此代碼而不是document.ready,如果您使用多個單頁面模板並且ajax轉換更多所以 - 在這種情況下,您還應該使用on函數而不是直播,以便僅偵聽當前頁面的冒泡事件。

那麼這將是類似於

$('#yourPage').on('change', 'input[name="radio-choice-1"]', function(){

+0

非常感謝,我採取這種方式 – bahamut100 2012-02-16 17:06:23

+0

您對jQuery Mob的意見ile幫助,而且真是一招。謝謝。 – Sameer 2013-09-01 19:26:20

1

ü需要綁定一個變化處理程序,並如下

$('input[name="radio-choice-1"]').live('change', function(){ 
var selectedValue = $(this).val(); 

$.post("save.php", {optionSelected: selectedValue}); 
}); 

其中save.php內容會觸發一個Ajax調用

<?php 
session_start(); 
$_SESSION['radio-choice-1-option'] = $_POST['optionSelected']; 
?> 
+0

好吧,它實際上類似於jQuery – bahamut100 2012-02-16 17:05:32