0

I'm having trouble with Flip Switch Toggles in jQuery. I need the buttons to toggle between HTTP_GET requests when pressed.

The HTML output (there's some PHP behind this)

<label for="device-2">Power outlet</label>
<select name="device-2" id="device-2" data-role="slider">
    <option value="off">Off</option>
    <option value="on">On</option>
</select>

I'm able to get the device powered on/off but I have to be able to make two separate HTTP_GETs (turnon and turnoff)

$("#device-2").on("change", function() {
    $.get( "functions/caller.php", { action: "turnon", device: "2" });
});

I've tried out this toggle function but it's just dead. It doesn't seem to make any HTTP requests when I've troubleshooted it in browser console

$("#device-2").toggle(function() {
    $.get( "functions/caller.php", { action: "turnon", device: "2" });
}, function() {
    $.get( "functions/caller.php", { action: "turnoff", device: "2" });
});

Could somebody please point me in the right direction?

4

2 回答 2

0

Try this:

$("#device-2").on("change", function() {
    var value = $(this).val();
    $.get( "functions/caller.php", { action: "turn" + value, device: "2" });
});

Assuming that your caller.php file accepts the actions "turnon" and "turnoff", this should dynamically change the $.get call based on the selected element's value.

于 2013-11-10T23:15:43.647 回答
0

Is this what you're looking for?

$("#device-2").on("change", function() {
    var state = $(this).val();
    $.get( "functions/caller.php", { action: state, device: "2" });
});
于 2013-11-10T23:16:56.943 回答