2014-02-13 167 views
1

我相信我的問題與語法相關。在選擇元素中找到一個選項並選擇它

我有一個頁面,其上有標籤(按鈕)。用戶應該能夠點擊其中一個標籤來編輯它的名字。

也在同一頁上,我有一個選擇列表,在其中跟蹤所有更改。我點擊標籤按鈕時出現問題,並且應該動態選擇列表中的相應選項。我試過幾件事情,這是我最新的嘗試:

HTML

 <!-- tags container --> 
     <div class="container_12"> 
      <div class="grid_2"><img src="images/spacer.png" /></div> 
      <div id="content" class="grid_8"> 
       <button name="PHP" class="tag-button" onclick="edit(this)">PHP</button> 
      </div> 
      <div class="grid_2"><img src="images/spacer.png" /></div> 
     </div> 
     <!-- tags container end --> 

     <!-- action buttons container --> 
     <div class="container_12 action-bar"> 
      <div class="grid_4"><img src="images/spacer.png" /></div> 
      <div id="action-add"> 
       <input type="text" name="newTag" id="newTag" /> 
       <input type="button" id="add" value="Add tag" /> 
      </div> 
      <div class="grid_4"><img src="images/spacer.png" /></div> 
      <div id="action-edit"> 
       <input type="text" name="editTag" id="editTag" /> 
       <input type="button" id="update" value="Update tag" /> 
      </div> 
     <!-- action buttons container end --> 
     </div> 

     <!-- Real Time list container --> 
     <div class="container_12"> 
      <div class="grid_4"><img src="images/spacer.png" /></div> 
      <select id="insertString" multiple> 
       <option value="0">PHP</option> 
      </select> 
     </div> 
     <!-- real time list container end --> 

使用Javascript/jQuery的:

//function edit: places the tag button info in textbox 
    function edit(element) 
    { 
      var name = $(element).attr('name'); 
      //add the tag name to the editTag textbox 
      $('#editTag').val(name); 
      //find in the list the corresponding option, and select it 
      $('#insertString').find('option: contains("' + name + '")').attr("selected", true); 
    } 

的代碼,我很擔心,現在的部分,是「找到列出相應的選項,並選擇它。「我似乎無法讓它選擇所有選項。

任何幫助,將不勝感激。

+0

試試這個:'$( '#insertString')找到( '選項:包含(「' + name +'「)')。prop(」selected「,true);' –

+0

我剛試過這個,列表中的項目還沒有被選中... – wribit

+0

可能的重複項:http:// stackoverflow.com/questions/314636/how-do-you-select-a-particular-option-in-a-select-element-in-jquery –

回答

1
var previous = "Gateway 1", edited = "world"; 
$('#insertString option:contains("' + previous +'")').text(edited); 
$('#insertString option:contains("'+ edited + '")').attr('selected', true); 

JSFiddle

+0

它仍然沒有被選中,改變我的代碼後 - 對不起。我甚至曾嘗試過... ...無效 – wribit

+0

請參閱編輯的答案。包括JSFiddle – Triode

+0

我不明白......你的小提琴工作正常,但是當我在我的最後嘗試它時 - 它不會。這是因爲我的選擇是多重? – wribit

0
//function edit: places the tag button info in textbox 
    function edit(element) 
    { 
      var name = $(element).attr('name'); 
      //add the tag name to the editTag textbox 
      $('#editTag').val(name); 
      //find in the list the corresponding option, and select it 
      $('#insertString').val('"+name+"'); 
    } 
+0

仍然是零歡樂...對不起 – wribit

0

我建議如下:

$('#insertString').find('option:contains("'+ name + '")').attr("selected", true); 
+0

這是我第一次嘗試,但我認爲我的選擇是多選的事實是它失敗的原因。任何想法如何使這個工作與多選擇將是偉大的 – wribit

0

$("#insertString option[value='" + name "']").prop('selected', true); 
+0

我有一種感覺,我的選擇是一個多選框,是這些答案不工作的原因 - 任何想法如何使這個工作與多選擇? – wribit

相關問題