我刚刚发现jquery-mobile,但我发现文档并不是真正适合“初学者”。
所以,我有一个带单选按钮的字段集。当用户“选中”一个按钮时,如何通过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>发布于 2012-02-17 00:47:46
让我们从一个简单的jQuery示例开始,您对jQuery有多熟悉?
您需要将一个处理程序绑定到单选按钮上的change事件,然后向PHP发出AJAX调用,在PHP中设置会话变量。
要绑定处理程序,请参阅on或live函数的jQuery文档,类似于$(document).ready...中的内容
$('input[name="radio-choice-1"]').live('change', function(){
var value = $(this).val();
$.ajax({
//see jQuery ajax functions
});
});但在jQuery手机上:
你应该使用jQM (jQuery移动版)的pageinit事件来运行这段代码,而不是document.ready,如果你正在使用多个单页面模板和ajax转换-在这种情况下,你也应该使用on函数而不是live,以便只监听当前页面的冒泡事件。
那就类似于
$('#yourPage').on('change', 'input[name="radio-choice-1"]', function(){
发布于 2012-02-17 00:53:44
您需要绑定一个更改处理程序并触发一个ajax调用,如下所示
$('input[name="radio-choice-1"]').live('change', function(){
var selectedValue = $(this).val();
$.post("save.php", {optionSelected: selectedValue});
});save.php内容的位置
<?php
session_start();
$_SESSION['radio-choice-1-option'] = $_POST['optionSelected'];
?>https://stackoverflow.com/questions/9315012
复制相似问题