首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JQuery:警告不能正常工作

JQuery:警告不能正常工作
EN

Stack Overflow用户
提问于 2013-08-29 23:50:22
回答 1查看 354关注 0票数 0

我有一个通过JQuery创建的对话框,如下所示

代码语言:javascript
复制
    <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>

但是,当我测试它时,警报消息会返回类似于“65未定义”和"undefinedDavid L.Kirp“之类的内容,而不是”65DavidL.Kirp“,这表明并不是同时初始化了两个变量。我怎么才能解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-30 00:02:35

在您的情况下,这个错误是完全合理的,因为当您更改时,instructor - $(this).closest('input[name=instructor]:checked')会给您检查的收音机,但是$(this).closest('input[name=common_name]:checked')将找不到元素,因为common_name无线电不是instructor的祖先。这种情况也可能发生,如果您选择一个common_name,指导员将给undefined

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

代码语言:javascript
复制
$('input:radio').change(function () {
    var instructor_id = $(this).closest('.update-dialog').find('input[name=instructor]:checked').val();
    var common_name = $(this).closest('.update-dialog').find('input[name=common_name]:checked').val();

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

演示:小提琴

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18522748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档