views:

67

answers:

3

I had a problem wirh my jquery code.My code work well except the alert part in IF condition.

    $('#datepicker').datepicker({ onSelect: function(dateStr) {
         var url = 'GetExchangeRate';
         var sellingRateValue;
         var buyingRateValue;
         $('#Duedate').val(dateStr);
         $.post(url, { dateToGet: dateStr },
    function(data) {

         var dr = $('#Dollar').val;
         var br = $('#BuyingRate').val;
         var sr = $('#SellingRate').val;
         if ((dr == null || dr.length < 1) && (br == null || br.length < 1) && (sr == null || sr.length < 1)) {
           alert("Please enter the exchage for today.");
            $('#Dollar').val('');
            $('#BuyingRate').val('');
            $('#SellingRate').val('');
            return false;
        }
        else {
            $('#Dollar').val(data.DollarRate);
            $('#BuyingRate').val(data.SellingRate);
            $('#SellingRate').val(data.BuyingRate);

            //alert(data.CurrentDate);


        }
    }, 'json');
     }
     });

Here is code part which is not working.

if ((dr == null || dr.length < 1) && (br == null || br.length < 1) && (sr == null || sr.length < 1)) {
           alert("Please enter the exchage for today.");
            $('#Dollar').val('');
            $('#BuyingRate').val('');
            $('#SellingRate').val('');
            return false;
+4  A: 

Try:

 function(data) {

         var dr = $('#Dollar').val();
         var br = $('#BuyingRate').val();
         var sr = $('#SellingRate').val();

with () at the end.

Sebastián Grignoli
+1 for good catch
Richa
@Sebastián: i don't what's the problem.....your suggestion code doesn't work..
Mik-Mark
Let's keep looking! (Use it with the parentheses anyway)
Sebastián Grignoli
@Mik-Mark - You definitely should use the parentheses for .val() (though it does do something w/o them) - http://api.jquery.com/val . Are you using Firebug http://getfirebug.com or some other JS debugger?
Peter Ajtai
+1  A: 

Though I am not entirely sure what it is that you want to do, the logic may be a bit off. You should apply Sebastian's fix first and possibly rethink the condition for your if statement.

if (     (dr == null || dr.length < 1)
      && (br == null || br.length < 1)
      && (sr == null || sr.length < 1)) {

    alert("Please enter the exchage for today.");
    $('#Dollar').val('');
    $('#BuyingRate').val('');
    $('#SellingRate').val('');
    return false;
}

The logic of the if means that only reset these fields if all the fields are empty, which doesn't seem to make sense to me. I'm guessing what you want to say is, "reset all of these fields as long as any one of them are empty."

if (     (dr == null || dr.length < 1)
      || (br == null || br.length < 1)
      || (sr == null || sr.length < 1)) {

Side note:

I think you should change your condition to,

if (!dr && !br && !sr) {...} // your original code

or

if (!dr || !br || !sr) {...} // With my fix
Anh-Kiet Ngo
I agree. @Mik-Mark, the lines inside the condition are resetting the contents of three input elements that you just found all empty in your example.
Sebastián Grignoli
@Sebastian - If those elements had no `value` attribute, I guess it would add empty `value` attributes... but that code in and of itself shouldn't cause the if to not trigger.
Peter Ajtai
@Peter Ajtai, the OP said the alert() part of the IF doesn't trigger. It might as well mean that the if was never true.
Anh-Kiet Ngo
Well of course, but that just means things are working fine. The possibility of some sort of syntax or other error outside that if should be eliminated.
Peter Ajtai
+1  A: 

First, like Sebastian wrote, you should use val() with parentheses.

Second, try this right before the if instead of what you have now:

     var dr = "";
     var br = "";
     var sr = "";

If the alert pops up now, then you know that it didn't pop up before because not all three of dr, br, and sr are being set to empty strings or null. (all three must be "" or null for the if to trigger).

If the alert still doesn't pop up then you have some other problem outside that if statement.

Make sure you look at your variables / code with Firebug.

Peter Ajtai