admin管理员组

文章数量:1398832

var id = "<?php echo $val->id; ?>";

var myRadio = $('input[name=<?php echo "mala".$val->id; ?>]').val();

if (myRadio.val.length > 0){
   $("#anketa").load('', 
       { answer: myRadio, id: id });
   }
});

Why this if (myRadio.val.length > 0) returns it looks like false, when i am sure that there is something in the string. If its not a good way, how to check that the string is empty or not?

var id = "<?php echo $val->id; ?>";

var myRadio = $('input[name=<?php echo "mala".$val->id; ?>]').val();

if (myRadio.val.length > 0){
   $("#anketa").load('http://www.svastara.rs/anketa/anketa_m1_data', 
       { answer: myRadio, id: id });
   }
});

Why this if (myRadio.val.length > 0) returns it looks like false, when i am sure that there is something in the string. If its not a good way, how to check that the string is empty or not?

Share Improve this question edited Oct 24, 2012 at 11:17 Parag Meshram 8,51110 gold badges56 silver badges88 bronze badges asked Oct 24, 2012 at 11:08 DanielaDaniela 1391 gold badge3 silver badges11 bronze badges 1
  • It shouldn't "look false", it should (probably) throw an error. Why are you trying to take val of val()? – raina77ow Commented Oct 24, 2012 at 11:10
Add a ment  | 

3 Answers 3

Reset to default 6

No need in val, since you should already get the value with val() method in the previous line:

if (myRadio.length > 0) { ... }
if(myRadio)
return true;
else
return false;

You don't need to use myRadio.val.length due to myRadio is already the value of that input. So all you need is to check this variable with length.

var id = "<?php echo $val->id; ?>";
var myRadio = $('input[name=<?php echo "mala".$val->id; ?>]').val();
if (myRadio.length > 0){
     $("#anketa").load('http://www.svastara.rs/anketa/anketa_m1_data', { answer: myRadio, id: id });
}

You can have a better explanation at http://www.w3schools./jsref/jsref_length_string.asp

Good luck!

本文标签: javascriptjQuery to check empty stringStack Overflow