开发者

Error get name value option_id in jquery

开发者 https://www.devze.com 2023-03-19 09:46 出处:网络
<scripttype=\"text/javascript\"> $(document).ready(function(){ $(\'#btnSubmit\').bind(\'click\', function(){
<script  type="text/javascript">
$(document).ready(function(){
   $('#btnSubmit').bind('click', function(){
      $('.hiddenId').each(function(){
         var id = $(this).val();
         var option = $('#option_' + id).val();
         if(!option){
            alert('Answer empty');
            return false;
         }
     });  
   )};
});
</script>
...
<input type="hidden" class="hiddenId" name="question[]" value="&开发者_如何转开发lt;?php echo $question->id ?>" />
<input type="radio" id="option_<?php echo $question->id ?>" value="<?php print_r($option[$i]); ?>" /><?php print_r($option[$i]); ?>
...

=> I can't get value id option_$i ($i is a array have value 1->n) in jquery


It's because you are using:

var id = $(this).val();

That will return the value of the item, not the ID. To get the ID of (this), use

var id = $(this).attr('id');


I found a mistake in your code, is this your problem?

<script  type="text/javascript">
$(document).ready(function(){
   $('#btnSubmit').bind('click', function(){
      $('.hiddenId').each(function(){
         var id = $(this).val();
         var option = $('#option_' + id).val();
         if(!option){
        alert('Answer empty');
            return false;
         }
     });  
   )}; // <= Error?
});
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消