开发者

My Jquery isn't "throwing" anything

开发者 https://www.devze.com 2023-03-26 02:45 出处:网络
I have a code here: <script type=\"text/javascript\" src=\"<?=base_url()?>lib/jquery-1.6.2.min.js\"></script>

I have a code here:

<script type="text/javascript" src="<?=base_url()?>lib/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$('.hover-star').click(function (){
    $('.someDiv').text(this.val());
})
</script>



                <input name="neat" type="radio" class="hover-star" value="1" title="Poor"/>
                <input name="nea开发者_StackOverflow中文版t" type="radio" class="hover-star" value="2" title="Fair"/>
                <input name="neat" type="radio" class="hover-star" value="3" title="Satisfactory"/>
                <input name="neat" type="radio" class="hover-star" value="4" title="Outstanding"/>

                <div id="someDiv">aray</div>  

My problem here is that it isn't throwing anything! I'm really in the corner here. In jsfiddle.net this is working. What I want is when my radio button is clicked it will show its value or title.


I see four things in your code:

  1. Don't forget the ; at the end of your jquery click event definition.

  2. You should use #someDiv instead of .someDiv as someDiv is an Object Id, not a Object class. This was already mentioned by Wesley.

  3. You can't use this.val() since val() is not a native javascript function. You may want to use $(this).val() which is wrapping the javascript object into jQuery.

  4. You need to use $(function() { /* your code */ }); as your javascript function is defined before your HTML object will be created. This is a shortcut for
    $(document).ready(function () { /* your code */ });

It would be:

<script type="text/javascript">
$(function() {
  $('.hover-star').click(function (){
    $('#someDiv').text($(this).val());
  });
});
</script>


You need to put your jQuery code in the "ready" handler:

$(document).ready(function() {
    // put your jQuery code here.
});


You should use $(this), not this.

See here.

0

精彩评论

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