2014-02-13 162 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

相关问题