为什么这段代码不起作用?

dat*_*ign -4 html jquery

我想在单击时显示span标记中两个单选按钮的值,但这不起作用:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Untitled Document</title>
        <script src="jquery.js" type="text/javascript">
        </script>
    </head>
    <body>
        <p>
            your amount is : <span id="displayPrice"></span>
        </p>
        <input type="radio" value="159" name="price" onclick="$('displayPrice').html(this.value)">
        <br>
        <input type="radio" value="259" name="price" onclick="$('displayPrice').html(this.value)">
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Gav*_*our 11

$('displayPrice')不是有效的选择器.你想要$('#displayPrice').

在事实之后,但是为了它的价值通常是一个好主意,分离你的JavaScript处理代码并将其放入一个准备好的事件,例如: -

$(document).ready(function() {

    $("input[name='price']").click(function() {
        $('#displayPrice').html($(this).val());
    });
});
Run Code Online (Sandbox Code Playgroud)