2009-11-02 118 views
5

我必须使用重音符号为特定注册商编码域名(IDNA)。在提交之前更改输入字段

我有一个简单的输入框:

<input type="text" id="idndomain" name="sld[0]" size="40" /> 

我的jQuery函数

$(document).ready(function() { 
    $('#domainform').submit(function(){ 
     $.getJSON("includes/idna/idna.php", { 
      domain: $("input#idndomain").val() 
     }, function(data){ 
      $("div#result").html($('<b>' + data.encoded + '</b>')); 
      $('#idndomain').val(data.encoded); 
     }); 
     return true; 
    }); 
}); 

,所以我将查询发送idna.php编码的域名,并返回一个JSON数组:

{"encoded":"xn--caf-dma.ch"} 

问题是表单正在用'原始'值取代,而不是返回的值b y的json查询。

问题是:如何首先等待json结果,用编码后的字符串替换输入字段,然后提交?

回答

7

尝试绑定到提交按钮,而不是形式,并明确调用JSON呼叫的成功回调中窗体的submit处理程序:

$(document).ready(function() { 
      $('#submitButton').click(function(){ 

        // maybe disable the submit button once clicked? 
        $(this).attr('disabled', true); 
        $.getJSON("includes/idna/idna.php", { 
          domain: $("input#idndomain").val() 
        }, function(data){ 
          $("div#result").html($('<b>' + data.encoded + '</b>')); 
          $('#idndomain').val(data.encoded); 
          // now submit the form 
          $('#domainform').submit(); 
        }); 
        return false; 
      }); 
    }); 
+1

一定要允许不仅仅是一个点击更多,有些人可能会提交一个带有return或tab> space/tab> return的表单。 – dylanfm 2009-11-02 11:16:36

+0

为什么不绑定到$(“selector”)。live(“click”,function(){}); ? 总是发现它更好,只是以防万一:) – Sam 2009-11-02 11:18:36

+0

我不认为有足够的上下文来推荐使用'live',它只有当提交按钮被替换时才需要(我不能根据问题看到发生) – karim79 2009-11-02 11:21:36

相关问题