修改后的代码版本:
<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>