jQuery从外部函数中选择变量

时间:2013-09-20 21:28:05

标签: jquery

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

如何才能让它获取相应的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>

Jquery的:

$("#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");
});

3 个答案:

答案 0 :(得分:1)

只需在侦听器外声明它,然后像这样修改点击:

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

同时删除对话框函数内的变量赋值: http://jsfiddle.net/NYWg2/9/

(单击是按钮查看“我”提醒)

答案 1 :(得分: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");
});

答案 2 :(得分: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);
});