2010-02-24 29 views
2

我有以下動態創建HTML塊:jQuery的VAL()不上動態地生成的輸入

<form class="standard settingsPage" method="post" enctype="multipart/form-data" name="account" style="background-color: rgb(61, 80, 133);"> 
<h2>Add New Account</h2> 
<p> 
<label class="" disabled="true">E-mail address:</label> 
<input id="accountEmailAddress" class="" type="text" value="" name="accountEmailAddress"/> 
</p> 
<p> 
<label class="" for="accountEmailPassword">Password:</label> 
<input id="accountEmailPassword" type="password" name="accountEmailPassword"/> 
</p> 
<p class="submit"> 
<input type="button" onclick="checkEmailSettings();" value="Send" name="submit"/> 
</p> 
<p> 
<label>Mail Server:</label> 
<input id="mail2server" type="text" name="mail2server"/> 
</p> 
<p> 
<label>Mail Type:</label> 
<select id="mail2type" name="mail2type"> 
</select> 
</p> 
<p> 
<label>Mail Security:</label> 
<select id="mail2security" name="mail2security"> 
</select> 
</p> 
<p> 
<label>Mail Server Port:</label> 
<input id="mail2port" type="text" name="mail2port"/> 
</p> 
<p> 
<label>Mail Username:</label> 
<input id="mail2username" type="text" name="mail2username"/> 
</p> 
<p class="submit"> 
<input id="mailsend" type="button" name="mailsend" onclick="checkEmailSettings();" value="Send"/> 
</p> 
</form> 

其被附加到一個現有的形式。

但是,當我做$('#mail2server')。val()時,它會返回空白,即使盒子裏有東西。如果我做$('#mail2server')。attr('name'),它會返回名稱,所以它肯定會識別該元素存在。任何想法,爲什麼這將失敗?

乾杯, Gazler。

編輯

function checkEmailSettings() 
{ 
    var emailAddress=$("#accountEmailAddress").val(); 
    var emailPassword=$("#accountEmailPassword").val(); 
    var datastring = "emailaddress="+emailAddress+"&emailpassword="+emailPassword; 
    if (additionalInfo == 1) 
    { 
     var mailserver = $("#mail2server").val(); 
     var mailtype = $("#mail2type").val(); 
     var mailsecurity = $("#mail2security").val(); 
     var mailport = $("#mail2port").val(); 
     var mailusername = $("#mail2username").val(); 
     alert($("#mail2server").val()); 
     datastring += "&mailserver="+mailserver+"&mailtype="+mailtype+"&mailsecurity="+mailsecurity+"&mailport="&mailport+"&mailusername="+mailusername; 
    } 
    $('input[type=text]').attr('disabled', 'disabled'); 
    $('input[type=password]').attr('disabled', 'disabled'); 
    $('input[type=button]').attr('disabled', 'disabled'); 
    $.ajax({ 
     type: "GET", 
     url: "checkemailsettings.php", 
     data: datastring, 
     async: true, 
     cache: false, 
     timeout:50000, 

     success: function(data){ 
     switch(parseInt(data)) 
     { 
       //SNIPPED 
     case 4: 
     alert("More information needed."); 
     if (additionalInfo == 0) 
     { 
     var string = addTextToForm("Mail Server","mail2server"); 
     string += addOptionsToForm("Mail Type","mail2type", new Array("IMAP", "POP3")); 
     string += addOptionsToForm("Mail Security","mail2security", new Array("NOTLS", "TLS", "SSL")); 
     string += addTextToForm("Mail Server Port","mail2port"); 
     string += addTextToForm("Mail Username","mail2username"); 
     string += addButtonToForm("Send","mailsend", "checkEmailSettings();"); 
     alert(string); 
     $('form[name=account]').append(string); 
     additionalInfo = 1; 
     } 
      break; 
     } 
     }, 
    }); 
} 

    function addTextToForm(strLabel, strID, strVal) 
    { 
    if (!strVal) {return "<p><label>"+strLabel+":</label><input id=\""+strID+"\" type=\"text\" name=\""+strID+"\" /></p>";} 
     return "<p><label>"+strLabel+":</label><input id=\""+strID+"\" type=\"text\" name=\""+strID+"\" value=\""+strVal+"\"/></p>"; 
    } 
    function addButtonToForm(strLabel, strID, functionName) 
    { 
     return "<p class=\"submit\"><input id=\""+strID+"\" value=\""+strLabel+"\" onclick=\""+functionName+"\" type=\"button\" name=\""+strID+"\"/></p>"; 
    } 

    function addOptionsToForm(strLabel, strID, optionsArr) 
    { 
     var returnstring="<p><label>"+strLabel+":</label><select id=\""+strID+"\" name=\""+strID+"\">"; 
     for (i=0; i<optionsArr.length; i++) 
     { 
      returnstring += "<option>"+optionsArr[i]+"</option>"; 
     } 
     returnstring += "</select></p>"; 
     return returnstring; 
    } 
+1

有沒有可能你有多個輸入元素的「ID」值「mail2server」? (或任何其他具有相同「id」值的元素?) – Pointy

+0

不可以。絕對只是一個。 – Gazler

+0

那麼好吧,那麼當你調用「document.getElementById('mail2server')」並查看「value」屬性時會發生什麼?這就是jQuery所要做的。 – Pointy

回答

2

「警戒」的號召說$('#mailserver'),不$('#mail2server')

+0

這是真的,但它仍然沒有檢索價值。感謝您指出了這一點。 – Gazler

+0

好吧有兩種可能:(1)你發現了一個瀏覽器錯誤,當你嘗試訪問javascript的值時,導致輸入元素的值不能正常工作;(2)當你獲取你的東西時認爲是有問題的輸入元素,你實際上得到了別的東西。從id爲「mail2server」的元素得到的元素類型是什麼? – Pointy

+0

如果我通過輸入標籤給它賦值(輸入id = mail2server value = whatever),那麼類型是[object Object]更加模糊,那麼它將返回值。 – Gazler

1

我創建的動態添加你上面有代碼示例頁面,一切工作就好了。還有其他事情可能發生,也許在你的提交按鈕正在調用的函數中?

+0

是的,它也適用於我的樣本,這讓我覺得這是與動態添加有關。歡呼輸入雖然,提交按鈕代碼添加到初始帖子。 – Gazler

1

我覺得這是很好的補充「返回false;」在輸入按鈕的onclick屬性的末尾。