2013-08-23 29 views
0

我需要的導航點KONTAKT下我的聯繫公式推上http://robert-richter.com幫助。我想發送這個表格與不同的主題。在下拉菜單中選擇Dependung on with option,我想創建一個單獨的主題。我想這樣做的原因是因爲我想在我的電子郵件帳戶中使用過濾器,以保存基於sibject標題的電子郵件。如何發送形式的內容與不同的主題

例子:

聯繫表姓名:John亞歷克斯選擇選項:Anfragepersönlich//德國私人請求

主題的郵箱:PersönlicheAnfrage·馮·亞歷克斯·約翰23/08/2013

過濾器'PersönlicheAnfrage馮',並將其保存在文件夾 'PersönlicheAnfragen'(ENG:私人請求)在

林全新的jQuery的。這是我到目前爲止:

<form action="#" method="post" title="Kontaktformular" class="ajax"> 
<div><label for="name">Name</label> 
    <input name="name" type="text" title="Name"></div> 
<div><label for="email">E-Mail-Adresse</label> 
    <input name="email" type="text" title="Email"></div> 
<div><label for="auswahl">Präfix</label> 
<label class="label"> 
    <select name="auswahl" class="dropdown"> 
    <option selected value="Webdesign">Anfrage Webdesign</option> 
    <option value="Persoenlich">Anfrage persönlich</option> 
    <option value="Andere">Alles andere</option> 
    <option value="Spam">Spam</option> 
    </select> 
    </label></div> 
    <div><textarea name="message" title="Nachricht"></textarea></div> 
<button value="Send" type="submit" class="button">Absenden</button> 
</form> 

//php file 
<?php 
if (isset($_POST['name'], $_POST['email'],$_POST['auswahl'], $_POST['message'])) { 
    print_r($_POST); 
} 

$('form.ajax').on('submit', function() { 
var that = $(this), 
    url = that.attr('action'), 
    type = that.attr('method'), 
    data = {}; 

that.find('[name]').each(function(index, value) { 
    var that = $(this), 
     name = that.attr('name'), 
     value = that.val(); 

    data[name] = value; 
}); 

$.ajax({ 
    url: url, 
    type: type, 
    data: data, 
    success: function(response) { 
     console.log(response); 
    } 
}); 
return false; 

});

http://jsfiddle.net/c5C8F/2/

如何添加所選擇的選項,而不是選擇元素?

+0

你'that.find ...'語句包含一個函數,誰擁有一個名爲參數'value'。在該函數內部,您聲明一個具有相同名稱的局部變量。雖然這與你的問題沒有直接關係,但我想我會指出,因爲它聞起來很麻煩。 – UweB

+0

更新http://jsfiddle.net/c5C8F/2/ –

回答

1

嘗試

$('form.ajax').on('submit', function() { 
    var that = $(this), 
     url = that.attr('action'), 
     method = that.attr('method'), 
     data = {}; 

    that.find('[name]').each(function(index, value) { 
     var that = $(this), 
      name = that.attr('name'), 
      value = that.val(); 

     data[name] = value; // need to assign value instead of name 
    }); 
    console.log(data);  
    return false; 
}); 

如果你想獲得所有被選中值PARAM列表嘗試

console.log(that.serialize()) 
+0

謝謝這個偉大的工作。你知道如何改變ajax命令來獲得選擇嗎?香港專業教育學院更新的jsfiddle代碼:http://jsfiddle.net/c5C8F/2/ –

1

試試這個:如果你想要做這樣你實現,那麼更新您的代碼像這個:

$('form.ajax').on('submit', function() { 
    var that = $(this), 
     url = that.attr('action'), 
     type = that.attr('method'), 
     data = {}; 

    that.find('[name]').each(function(index, value) { 
     var that = $(this), 
      name = that.attr('name'), 
      value = that.val(); 

     var isSelect = that.find("select"); 
     if(isSelect.length > 0){ 
      value = isSelect.val(); 
     } 

     data[name] = value; 
    }); 
    console.log(data); 
    return false; 
}); 

但事情是,你正在經歷每個輸​​入的形式和存儲數組中的keyValue對中的數據。如果你只是更新代碼下面這樣那麼你不必因爲jQuery不會爲你做手工:

$('form.ajax').on('submit', function() { 
    var that = $(this), 
     url = that.attr('action'), 
     type = that.attr('method'), 
     data = {}; 

    data = that.serialize(); //it will return each input in string concatenation 

    //Or, 

    data = that.serializeArray(); //it will return each input in object array 

    console.log(data); 
    return false; 
}); 

這是你的哪種方法最適合你的選擇。

我希望它能幫助

+0

更新http://jsfiddle.net/c5C8F/2/ –

+0

@RobertRichter ::你在它添加$就調用..所以做什麼你想現在做什麼? – maverickosama92

+0

我不知道如何做到這一點右鍵,選擇= $(本).find( 「選擇[名稱= 'auswahl']」), –

0

獲取所選選項的值,你可以這樣做:

// Get the select box. 
var $select = $(this).find("select[name='auswahl']"); 
// Get the selected option. 
var $selectedOption = $select.find(":selected"); 
// Get the value of the selected option. 
var selectedOptionValue = $selectedOption.val(); 
+0

更新http://jsfiddle.net/c5C8F/2/ –

+0

當我使用VAR selectedOptionValue = $ selectedOption.val();我得到一個ReferenceError:selectedOption沒有被定義爲http:// jsfiddle。net/c5C8F/7/ –

+0

@RobertRichter:如果你仍然想這樣做,那麼我更新你的小提琴看到這裏:http://jsfiddle.net/c5C8F/23/ – maverickosama92