如何从 jQuery 对话框按钮部分内部修改当前选择器的 id 属性


How do you modify the id attribute of current selector from inside of a jQuery dialog button section?

我想将"间接"类 id 中的用户名字符串替换为用户在 jquery 对话框输入字段中指定的用户名。问题是我不能使用 this.id = this.id.replace('用户名', myvalue) 因为$this将引用 jquery 对话框 ui 内的输入元素。请帮助我通过任何其他方法解决这个问题

<img src="images/AOL_button.png" id='http://openid.aol.com/username' class="indirect" />
<img src="images/google_button.png" id='https://www.username.google.com/accounts/o8/id' class="direct"/> 
================================================================================================================================================================================================================================================

==

$(.indirect).click(function(){
   $("#dialog").dialog({
     buttons: {
            "OK": function() {
                if($('#username').val() == '') {
                    $('#username').focus();
                } else {
                    var myvalue= $("#username").val();
                    var provider_url_post= // replace "username" in google id with myvalue
                    alert(provider_url_post);
})

你的JS中有一堆语法错误,但假设这些错误在你的实际代码中不存在:

$('.indirect').click(function() {
    var self = this;
    $("#dialog").dialog({
        buttons: {
            "OK": function() {
                if (!$('#username').val()) { // just check the truthiness
                    $('#username').focus();
                } else {
                    var myvalue = $("#username").val();
                    self.id = self.id.replace('username', myvalue);
                }
            }
        }
    });
});​