同阿贾克斯称为twice..JQuery阿贾克斯、twice、JQuery

2023-09-10 14:15:21 作者:Turn (转身)

我在JQuery..I米的问题,使用 $。阿贾克斯()在我的code (功能1)发送字段名和序列号到由 $ _ POST ['名称'] $ _ POST获取数据的ctrller ['sequenceno'] 并更新字段名在表中序列中没有given..And产生preVIEW显示面板与插入的领域.. 现在我想改变字段名再次针锋相对是现在,当我点击生成的显示面板领域,相应的设置会打开,我会尝试改变字段的名字了(功能2 )

i have a question in JQuery..I m using $.ajax() in my code (Function 1)to send the fieldname and the sequence number to the ctrller which gets its data by $_POST['name'] and $_POST['sequenceno'] and updates the fieldname in the table with sequence no given..And generates the Preview Display Panel with the inserted fields .. Now I'm trying the change the field name again tat is Now when I click on the generated display panel fields, the corresponding settings will open and i will try to change the name of the field now(Function 2)

两个功能1 功能2 相同..In的功能1 我要送的字段名和顺序没有

Both Function1 and Function2 are same ..In the Function1 I'm sending the fieldname and the sequence no

在功能可按2,我想送相同的字段名和(但其他值)sequenceno ..

In the Funciton 2,i want to send the same fieldname and (but the other value)sequenceno..

但在功能1(sequenceno是计数器值) 而在功能2(sequenceno是点击DIV ID(显示面板))

But in Function1 (sequenceno is the counter value) Whereas in the Function2 (sequenceno is the clicked div id (display Panel))

我如何可以利用相同的FUNC键的两个..或者可能需要使用单独的funcs中。

How can i make use of the same func for both ..Or could i need to use separate funcs..

即使我试着用2个独立的funcs中具有不同的URL,但不会正确更新

Even i tried with 2 separate funcs with different urls but not updated correctly

我的code是

    //This is what i insert the field initially

              $(".TextFieldSettings #fieldTitle").change(function (){
             fieldname=$(".TextFieldSettings #fieldTitle").val();
    $.ajax({
           type: "POST",
           url: "./server",
        data: "name="+fieldname+"&sequenceno="+counter,

                success: function(msg){
                      }//success
           });//ajax

     });//change

//After inserting to get the updated values in JSON format

     var htm = $.ajax({
       type: "GET",
          url: "./viewforms",
         async: false
       }).responseText;
        var myObject = eval('(' + htm + ')');


    gettype=myObject.attributes[0]["type"];
   getlabel=myObject.attributes[0]["labels"];

    //showing in my DisplayPanel view

            $("#labelstr"+counter+"").html(getlabel);
     });//change

现在     当我点击DisplayPanel视图

Now When i click the DisplayPanel view

      $("#displayPanel div").live("click", function(){
                             div_id=$(this).attr("id");

                                var htm = $.ajax({
                                  type: "GET",
                                  url: "./getattributes/"+div_id+"",
                                  async: false
                                 }).responseText;
                                var myObject = eval('(' + htm + ')');


                                gettype=myObject.attributes[0]["type"];
                                getlabel=myObject.attributes[0]["labels"];
                                getsize=myObject.attributes[0]["size"];

    if(gettype=='Text')
     {
    $(".TextFieldSettings").show(function(){
    $(".TextFieldSettings #fieldTitle").val(getlabel);//showing the updated value
                             if(getsize=='100')
                           {
             $("#fieldSize option:contains(Small)").attr("selected",true);
                           }
                             else if(getsize=='200')
                            {
        $("#fieldSize option:contains(Medium)").attr("selected",true);
                         }
              else 
            {
          $("#fieldSize option:contains(Large)").attr("selected",true);
            }
//Again i m changing the fieldname


       $(".TextFieldSettings #fieldTitle").change(function (){
      fieldname=$(".TextFieldSettings #fieldTitle").val();


                                alert(div_id);
                                        $.ajax({
                                           type: "POST",
                                           url: "./editsettings",



                                                                         data: "name="+fieldname+"&sequenceno="+div_id,
                                           success: function(msg){

                                            }//success
                                           });//ajax


});//change in text value later*/



                    });//show
                    }//if type = TEXT

                });//displaypanel Div clicked

但现在,如果我试图改变字段名再次声明,我写另一篇文章的功能 editsettings 但执行来自内部的 FUNC1 (改变开始),而不是 FUNC2 (改变字段名再次)... 请任何人离开这个答案.... 注意: $(。TextFieldSettings #fieldTitle)。改变()被使用了两次在我的PRG ..May是因为这样的updation出错

But now if I try to change the fieldname again I'm writing another POST function editsettings but the execution comes inside Func1(changing initially) instead of Func2(changing in the fieldname again)... Please anyone get out the answer for this.... Note: $(".TextFieldSettings #fieldTitle").change() is used twice inside my prg ..May be because of this the updation goes wrong

推荐答案

这似乎是问题是,无论你的事件处理程序被解雇,而您只需要后者开火。

It seems like the problem is that both of your event handlers are firing, and you only want the latter one to fire.

jQuery的.change()函数的添加的事件处理程序change事件。它不会取代现有的。如果你想删除previous处理程序,你需要的东西,如:

The jQuery .change() function adds an event handler to the change event. It doesn't replace existing ones. If you want to remove the previous handler, you need something like:

$(".TextFieldSettings #fieldTitle").unbind('change')

您可以将新的处理程序之前。

before you attach a new handler.

请注意,我不知道这工作(我只是发现它的API文档),我不能马上进行测试。但是,一般的想法是,如果你想要一个事件处理程序停止响应的事件,你必须删除处理程序。

please note that I'm not sure this works (I just found it from the api docs) and I can't test it right now. However, the general idea is that if you want an event handler to stop responding to an event, you have to remove the handler.