以下功能对jQuery Mobile网站执行相同操作,翻转切换按钮,将其用于。
<script>
function updateControls() {
$.getJSON('/status.json', function(json) {
$.each(json, function(key, value) {
$('#'+key).val(value).slider('refresh');
});
});
}
$('select').bind('change', function(event) {
element = event.target.id;
if (element.substr(0, 6) == 'outlet') {
eleid = element.substr(6, 2);
}
else {
eleid = 0;
}
command = event.target.value;
$.get('/cmd', { 'eleid' : eleid, 'cmd' : command });
});
$('#page1').bind('pageinit', updateControls);
</script>
答案 0 :(得分:3)
在document.ready
内包换代码也使用.on
代替.bind
,因为.bind
已被弃用。
$('#page1').on('pageinit', updateControls);
答案 1 :(得分:0)
代码的改编版本:
<script>
function updateControls() {
$.getJSON('/status.json', function (json) {
$.each(json, function (key, value) {
$('#' + key).val(value).slider('refresh');
});
});
}
$('select').on('change', function (event) {
element = event.target.id;
if (element.substr(0, 6) == 'outlet') {
eleid = element.substr(6, 2);
} else {
eleid = 0;
}
command = event.target.value;
$.get('/cmd', {
'eleid': eleid,
'cmd': command
});
});
$(document).ready(function () {
updateControls();
});
</script>
修改强>
如果你想使用提交按钮和选择一样,代码将是:
<script>
function updateControls() {
$.getJSON('/status.json', function (json) {
$.each(json, function (key, value) {
$('#' + key).val(value).slider('refresh');
});
});
}
$('select').on('change', function (event) {
element = event.target.id;
if (element.substr(0, 6) == 'outlet') {
eleid = element.substr(6, 2);
} else {
eleid = 0;
}
command = event.target.value;
$.get('/cmd', {
'eleid': eleid,
'cmd': command
});
});
$('input[type=submit]').on('click', function (event) {
element = event.target.id;
if (element.substr(0, 6) == 'outlet') {
eleid = element.substr(6, 2);
} else {
eleid = 0;
}
command = event.target.value;
$.get('/cmd', {
'eleid': eleid,
'cmd': command
});
$(document).ready(function () {
updateControls();
});
</script>
编辑2
如果您希望按钮点击事件:
<script>
function updateControls() {
$.getJSON('/status.json', function (json) {
$.each(json, function (key, value) {
$('#' + key).val(value).slider('refresh');
});
});
}
$('select').on('change', function (event) {
element = event.target.id;
if (element.substr(0, 6) == 'outlet') {
eleid = element.substr(6, 2);
} else {
eleid = 0;
}
command = event.target.value;
$.get('/cmd', {
'eleid': eleid,
'cmd': command
});
});
$('input[type=submit]').on('click', function (event) {
updateControls();
});
</script>