JQuery:警报无法正常工作

时间:2013-08-29 23:50:22

标签: jquery

我有一个像JQuery这样的对话框

    <div class ="editable" id="div_David L. Kirp[instructor_status]"contenteditable="1">
        <span class="text-error">Error: More than one user with same fname and lname</span<br/>
            Users:<br/>
        <span class="multiple-users">
                &nbsp[CLICK HERE] Instructor ID: 65, Common Name: David Kirp</span<br/>  
        <span class="multiple-users">
                &nbsp[CLICK HERE] Instructor ID: 17210, Common Name: David L. Kirp</span><br/><div class="update-dialog" title="Update Common Name">Which instructor do you want to update?<p><input type="radio" id="instructor_65" name="instructor" value="65"/><label for="instructor_65">        
                Instructor ID: 65, Common Name: David Kirp
            </label></p>
            <p><input type="radio" id="instructor_17210" name="instructor" value="17210"/>
<label for="instructor_17210">        
            Instructor ID: 17210, Common Name: David L. Kirp
            </label></p>Which common name do you want to assign the instructor?<p><input type="radio" id="commonName_65" name="common_name" value="David Kirp"/><label for="commonName_65">
            David Kirp
            </label></p><p><input type="radio" id="commonName_17210" name="common_name" value="David L. Kirp"/><label for="commonName_17210">
            David L. Kirp
            </label></p></div><button class="update-button" type="button">Update Common Name of an Instructor</button></div>

<script>
$("div.update-dialog").dialog({
                autoOpen: false,
                dialogClass: 'dialogStyle',
                resizable: false,
                modal: true,
                buttons: {
                    "Update": function() {
                    //$.load('update_common_name.php', 
                        $( this ).dialog( "close" );
                    },
                    Cancel: function() {
                        $( this ).dialog( "close" );
                    }
                }
           });



         $('div.editable').on('click', '.update-button', function () {
            $(".update-dialog").dialog("open");

         }); 

and I want it so that when I click on one of the radios, it will update the existing values to variables instructor_id and common_name (I eventually want to make an ajax request with them), like so: 

$('input:radio').change(function () {        
            instructor_id = $(this).closest('input[name=instructor]:checked').val();
            common_name = $(this).closest('input[name=common_name]:checked').val();

            // alert is for testing
            alert(instructor_id + common_name);
}); 
</script>

然而,当我测试它时,警报消息返回类似“65undefined”和“undefinedDavid L. Kirp”而不是“65David L. Kirp”的内容,表明这两个变量都没有被初始化。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

错误在您的案例中非常合理,因为当您更改instructor时,$(this).closest('input[name=instructor]:checked')会为您提供已检查的广播,但$(this).closest('input[name=common_name]:checked')将无法找到该元素,因为{{1} } radio不是common_name的祖先。它也可能以其他方式发生,如果您选择instructor,教师会给common_name

尝试(在您从两组值中选择值之前,仍然可以为其中一个字段提供未定义的内容)

undefined

演示:Fiddle