2013-04-29 77 views
3

使用select2 jQuery PlugIn我創建了一個標記框。所以我用下面的HTML表單代碼:Select2不會創建名稱屬性

<form action="someAction" method="post"> 
    <fieldset> 
     <select id="tags" multiple="" name="tags"> 
      <option value="tag1">val1</option> 
      <option value="tag2">val2</option> 
      <option value="tag3">val3</option> 
     </select> 
     <button type="submit" class="btn">Submit</button> 
    </fieldset> 
</form> 

選擇二,從它產生以下字段:

<input type="text" class="select2-input" autocomplete="off" id="s2id_autogen1" style="width: 10px;"> 

的問題就是在這裏name屬性丟失。如果沒有name屬性,我怎樣才能從PHP中的$_POST變量中獲取輸入字段的數據/文本?我怎樣才能管理這個?

的設置JS如下:

$(document).ready(function() { 
$("#tags").select2({ 
    placeholder: "Insert tag", 
    allowClear: true, 
    minimumInputLength: 2, 
    maximumSelectionSize: 1, 
    minimumWidth: 200 
}); 

});

+0

Select2爲視覺體驗創建元素。你使用的所有東西都一樣。這意味着實際的工作應該發生在原始元素上(在你的情況下它是具有name =標記的元素),Select2創建的元素僅用於向你顯示選定的選項,之後它將靜默地修改目標元素。 TL; DR - 只需通過$ .serialize序列化表單即可。 – 2013-04-29 10:46:17

+0

實際上它不會更改目標元素(此處爲name =標記)。我期望像''但這不會出現。我該如何獲得選定的? – tester 2013-04-29 11:06:20

+0

好的,我發現並不一定是'selected'屬性。它現在工作thx。 – tester 2013-04-29 11:13:38

回答

0

後需要命名的工作,但是我發現它有一個id:

id="s2id_autogen1" 

使用JS搶按id的值,並張貼。

如果ID是不斷變化的,你可以使用類:

HTML:

<form action="someAction" method="post"> 
<fieldset> 
    <!-- This will contain the value from the generated input --> 
    <input type = 'hidden' name = 'myField' id = 'myField' /> 
    <select id="tags" multiple="" name="tags"> 
     <option value="tag1">val1</option> 
     <option value="tag2">val2</option> 
     <option value="tag3">val3</option> 
    </select> 
    <button type="submit" class="btn">Submit</button> 
</fieldset> 

JS

$(document).ready(function(){ 
     $("#tags").select2({ 
     placeholder: "Insert tag", 
     allowClear: true, 
     minimumInputLength: 2, 
     maximumSelectionSize: 1, 
     minimumWidth: 200 
}); 

//Make it so everytime the input with the class select2-input value is changed 
//the value gets copied into the element with the id myField 
$('input.select2-input').live('change', function(){ 
    $('input#myField').val($(this).val()); 
}); 

});

就像這樣提交,表單將自動包含值爲int的POST變量。

0

我已經快速瀏覽了select2 Github賬戶中的一些票據,其中有幾位提到了這個問題。看起來select2只是不添加name屬性。

你可以嘗試通過JS的形式提交動態加一,看看是否能工程,

$('form').submit(function() { 
    $('#s2id_autogen1').attr('name', 'whatever'); 
    return true; 
}); 

或者你可以通過ajax提交表單,並通過ID搶值,並張貼這樣

0

經過多次閱讀,我決定改變select2.js本身。

在行2109改變它

this.focusser.attr("id", "s2id_"+this.select.context.id); 

如果您輸入的標籤是這樣

<select id="fichier"> 

因此是通過列表搜索你輸入的標籤將有s2id_fichier_search的ID

據我所知,不應該有衝突,這將允許您在同一頁上有多個select2和ru通過他們的活動來完成你的功能。

$(function() { 
    $('#s2id_fichier_search').keyup(function() { 
    console.log('Be Practical') 
    }) 
} 

因此,這將運行一樣,如果你使用

<select id="fichier" onkeyup="console.log('Be Practical')"> 
0

Definetly我解決了這個簡單的代碼片段。

我添加了一個隱藏輸入:

<input type="hidden" id="manufacturer" name="manufacturer" value="1">

而且在選擇一個ID:

<select class="full-width required" data-placeholder="Select Country" data-init-plugin="select2" required id='myselect'>

然後,只需添加一個簡單的JS在同一頁面更改隱藏值每次用戶更改值:

<script type='text/javascript'> 
 
    $(function() { 
 
     $('#myselect').change(function() { 
 
      // if changed to, for example, the last option, then 
 
      // $(this).find('option:selected').text() == D 
 
      // $(this).val() == 4 
 
      // get whatever value you want into a variable 
 
      var x = $(this).val(); 
 
      // and update the hidden input's value 
 
      $('#manufacturer').val(x); 
 
      alert("cambiado"); 
 
     }); 
 
    }); 
 
    </script>

0

這是一個老問題,但對於當前選擇2 4.XX版本正確的答案是集名稱屬性爲陣列

<select id="tags" multiple="" name="tags[]"> 

,然後你可以得到陣列$ _ POST這樣的:

$_POST['tags'] 
相關問題