Constantine
Constantine

Reputation: 783

How use if from a value of a select JQuery

I have this select form

<select id="measurement" name="measurement">
<option value="" selected>Select Measurement</option>
<option value="1" selected>Measurement 1</option>
<option value="2" selected>Measurement 2</option>
</select>

and I get its selection with this

$(document).ready(function () {
    $('#measurement').on('change', function () {
        var selected = $(this).find('option:selected').val();
        if (selected === 1) {
            $.ajax({
                url: "/Kostas/measurementData.php",
                type: "POST",
                data: {UserID: <?php echo json_encode($UserID); ?>, MeasurementID: selected},
                success: function (data, selected) {
                   //some code

                },
                error: function (data) {

                }
            });
        }
    });
});

My if statement does not work, while the MeasurementID: selected passes the correct value when I remove the if, it isn't the same case with the if statement, it doesn't recognise it as 1 when it is. How do I do this right?

Upvotes: 0

Views: 31

Answers (1)

Constantine
Constantine

Reputation: 783

Thanks to Rory McCrossan

val() returns a string so use == 1 or === '1'

Upvotes: 1

Related Questions