为什么jQuery不能形成.submit()调用封闭函数?

时间:2013-05-13 17:53:25

标签: jquery forms submit

我正在尝试在表单提交上调用命名函数。这是代码:

$(document).ready(function() {
  showPartnerSettings = function(e) {
    e.preventDefault();

    var $dialogForm = $("<div />")
    .attr("id", "partner-settings-form")
    .append($loading.clone())
    .load(envPath + "/partner/settings?partnerid="+e.data.partnerId, null, function(){ $("#partner-settings-form").css("display", "block"); })
    .dialog({
      title: "Partner Settings",
      modal: false, 
      resizable: false,
      width: 580, //CPB 04.11.13
      position:['middle',130],
      "close" : function(){ 
        var dialogid=$(this).parent("div").attr("id");
        $("#Tabs ul li."+dialogid).remove();
        $(this).remove();
        $("#alertmod").remove();
        //$link.removeClass('preventclick');
      },
    })        
    .dialog("open")
    .css("display", "block");

    return false;
  };

$("#partner-settings-form").submit(function(event) { return showPartnerSettings(event); });

当我在showPartnerSettings()上设置断点时,在.submit()函数执行后它不会中断。为什么呢?

编辑:

这是HTML Jasen所要求的:

<div id="partner-settings-form" style="display: block; width: auto; min-height: 66.6875px; height: auto;" class="ui-dialog-content ui-widget-content" scrolltop="0" scrollleft="0">         
<article class="sign_in form-dialog clearfix">
      <div class="bg-form-dlg">
        <div class="col_100 clearfix">
<h2>UPS Shipping Information</h2>
<form enctype="application/x-www-form-urlencoded" class="sign_in_form sign_up_form" action="" method="post"><div class="zend_form">
<div class="shippingdetails"><fieldset id="fieldset-account">
<div id="key-label" class="formlabel"><label for="key" class="required">Access Key: *</label></div>
<div id="key-element" class="formcontent">
<input type="text" name="key" id="key" value="CCA574143AD81C1A "></div>
<div id="id-label" class="formlabel"><label for="id" class="required">User ID: *</label></div>
<div id="id-element" class="formcontent">
<input type="text" name="id" id="id" value="argoworks"></div>
<div id="password-label" class="formlabel"><label for="password" class="required">Password: *</label></div>
<div id="password-element" class="formcontent">
<input type="text" name="password" id="password" value="Ubond007gun"></div>
<div id="account_number-label" class="formlabel"><label for="account_number" class="required">Account Number: *</label></div>
<div id="account_number-element" class="formcontent">
<input type="text" name="account_number" id="account_number" value="8X6124"></div>
<div id="address1-label" class="formlabel"><label for="address1" class="required">Address1: *</label></div>
<div id="address1-element" class="formcontent">
<input type="text" name="address1" id="address1" value="112.5 South La Brea Ave."></div>
<div id="address2-label" class="formlabel"><label for="address2" class="optional">Address2:</label></div>
<div id="address2-element" class="formcontent">
<input type="text" name="address2" id="address2" value=""></div>
<div id="phone-label" class="formlabel"><label for="phone" class="required">Phone: *</label></div>
<div id="phone-element" class="formcontent">
<input type="text" name="phone" id="phone" value="2147483647"></div>
<div id="city-label" class="formlabel"><label for="city" class="required">City: *</label></div>
<div id="city-element" class="formcontent">
<input type="text" name="city" id="city" value="Los Angeles"></div>
<div id="state-label" class="formlabel"><label for="state" class="required">State (code): *</label></div>
<div id="state-element" class="formcontent">
<input type="text" name="state" id="state" value="CA"></div>
<div id="country-label" class="formlabel"><label for="country" class="optional">Country:</label></div>
<div id="country-element" class="formcontent" style="">
<select name="country" id="country" class="dlg-select">
    <option value="US" label="UNITED STATES" selected="selected">UNITED STATES</option>
    <option>...etc...</option>
</select></div>
<div id="postal-label" class="formlabel"><label for="postal" class="required">Postal Code: *</label></div>
<div id="postal-element" class="formcontent">
<input type="text" name="postal" id="postal" value="90036"></div></fieldset></div>
<div>
<p><label id="configship-label">&nbsp;</label>
<input type="submit" name="configship" id="configship" value="Save Information"></p></div></div></form></div>
</div>
</article>

    UPS342hi</div>

我在你的帮助下发现了这个问题。我没有将.submit()附加到表单元素。现在我试图解决这个问题,而且我遇到了另一个问题。你知道我如何从JS访问POSTed表单数据吗?我需要访问名为&#34; partnerId&#34;的隐藏字段。在形式。表单数据是否包含在event.data中?感谢您的继续帮助。

1 个答案:

答案 0 :(得分:0)

如果您以完全动态的方式创建partner-settings-form(首次加载页面时html标记不存在),那么您的submit事件注册将不会绑定到表单。< / p>

你需要稍微改变一下:

$("body").on("submit", "#partner-settings-form", showPartnerSettings);

此示例使用body,理想情况下,您将选择一个在加载时静态的元素选择器,尽可能靠近对话框。

注意:

  1. 如果您在提交时没有做任何其他事情,那么您可以摆脱匿名功能。
  2. 您在partner-settings-form的提交活动中构建partner-settings-form似乎很奇怪。