Error get name value option_id in jquery
<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>
精彩评论