0

在我的对话框函数中,我需要将 I 设置为被点击的类的“id”(例如,当点击第一个“+ Talk”时,它应该设置 I = 100006。使用我当前的脚本,它将设置 I 为'reqtalk对话框'。

我怎样才能得到它来取而代之的 id 呢?

注意:在我将其放入对话脚本之前,当前脚本用于获取正确的 id。此外,如您所见,我无法将其设置为全局变量,因为它需要根据单击的内容进行更改。

小提琴:http: //jsfiddle.net/MCam435/NYWg2/8/

HTML:

<div id="reqtalkdialog" title="Confirmation Required">
  Would you like to request contact?
</div>

<div id="reqtalk100006" style="display:block"><a href="#" class="reqtalk" id="100006">
<span class="reqtalk_b"> + Talk </span></a> - 100006</div>

<div id="reqtalk100001" style="display:block"><a href="#" class="reqtalk" id="100001">
<span class="reqtalk_b"> + Talk </span></a> - 100001</div>

查询:

$("#reqtalkdialog").dialog({
   autoOpen: false,
   modal: true,
   buttons : {
        "Yes" : function() {
            var element = $(this);
            var I = element.attr("id");
            var info = 'id=' + I;

            $.ajax({
                type: "POST",
                url: "resource/talkrequest.php",
                data: info,
                success: function(){}
            });

            $("#reqtalk"+I).hide();
            $("#talking"+I).show();
            //return false;

            $(this).dialog("close");           
            },

        "Cancel" : function() {
          $(this).dialog("close");
        }
      }
    });

$(".reqtalk").on("click", function(e) {
    e.preventDefault();
    $("#reqtalkdialog").dialog("open");
});
4

3 回答 3

1

只需在侦听器之外声明它并像这样修改点击:

$(".reqtalk").on("click", function(e) {
    I = $(this).attr('id');
    e.preventDefault();
    $("#reqtalkdialog").dialog("open");
});

还要删除对话框函数内的变量分配:http: //jsfiddle.net/NYWg2/9/

(单击“是”按钮以查看“我”警报)

于 2013-09-20T21:36:17.237 回答
1

您可以使用对话框选项:

var element = $(this);
var I = element.dialog('option', 'currentId');

在事件中:

$(".reqtalk").on("click", function(e) {
    e.preventDefault();
    $("#reqtalkdialog").dialog('option', 'currentId', $(this).attr('id')).dialog("open");
});
于 2013-09-20T21:41:08.640 回答
1

我更新了你的 jsfiddle,下面是我的工作,可能不是最好的方法,但它可以工作。

HTML

<div id="reqtalkdialog" title="Confirmation Required">
  Would you like to request contact?
</div>

<div id="reqtalk100006" style="display:block"><a href="#" class="reqtalk" id="100006"><span class="reqtalk_b"> + Talk </span></a> - 100006</div>

<div id="reqtalk100001" style="display:block"><a href="#" class="reqtalk" id="100001"><span class="reqtalk_b"> + Talk </span></a> - 100001</div>
<input type="hidden" id="clickedNumber" value=""/>

JS

$("#reqtalkdialog").dialog({
    autoOpen: false,
    modal: true,
    buttons: {
        "Yes": function() {
            var numberToHide = $('#clickedNumber').val()
            //alert($('#clickedNumber').val());
            var element = $(this);
            var I = element.attr("id");
            var info = 'id=' + I;
            $.ajax({
                type: "POST",
                url: "resource/talkrequest.php",
                data: info,
                success: function() {}
            });
            $("#reqtalk" + numberToHide).hide();
            $("#talking" + numberToHide).show();
            //return false;
            $(this).dialog("close");
        },
        "Cancel": function() {
            $(this).dialog("close");
        }
    }
});
$(".reqtalk").on("click", function(e) {
    e.preventDefault();
    $("#reqtalkdialog").dialog("open");
    $('#clickedNumber').val(this.id);
});
于 2013-09-20T21:44:48.047 回答