Jquery html 不工作不知道为什么?

标签 jquery html jquery-ui jquery-mobile jquery-selectors

这是html

<!DOCTYPE html>
    <html>
    <title>Ingredient</title>
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="themes/receta.min.css" />
      <link rel="stylesheet" href="css/jquery.mobile.structure-1.1.0.min.css" /> 
      <script src="js/jquery-1.7.1.min.js"></script> 
      <script src="js/jquery.mobile-1.1.0.min.js"></script> 
      <script type="text/javascript" charset="utf-8" src="js/ingredients.js"></script>  

      <h1><center>INGREDIENTES</h1>
    <ul>
     <li onclick="this.parentNode.removeChild(this);">
      <input type="hidden" name="ingredients[]" value="Cheese" />
      Cheese
     </li>
     <li onclick="this.parentNode.removeChild(this);">
      <input type="hidden" name="ingredients[]" value="Ham" />
      Ham
     </li>
    </ul>
    <select onchange="selectIngredient(this);">
     <option value="Cheese">Cheese</option>
     <option value="Olives">Olives</option>
     <option value="Pepperoni">Pepperoni</option>
     <option value="Milk">Milk</option>
    </select>

      </html>

这是jquery

    function selectIngredient(select)
    {
      var $ul = $(select).prev('ul');

      if ($ul.find('input[value=' + $(select).val() + ']').length == 0)
        $ul.append('<li onclick="$(this).remove();">' +
          '<input type="hidden" name="ingredients[]" value="' + 
          $(select).val() + '" /> ' +
          $(select).find('option[selected]').text() + '</li>');
    }

This is what i need to see when yo select another ingredient this one should be putted with the other ones

当我运行这个 html 和 jquery 时它不起作用。如果我选择它,它应该将其他成分添加到列表中,但它不会发生。

最佳答案

我想这里:

$(select).find('option[selected]').text()

你实际上是这个意思:

$(select).find('option:selected').text()

但是为什么不再使用$(select).val()呢?在 HTML 上,文本和值是相同的。

关于Jquery html 不工作不知道为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13281352/

相关文章:

javascript - 使用 "this"作为html类的点击事件

javascript - 如何使用 Javascript 将小数转换为百分比

html - 如何在使用 jquery mobile 按住该 div 时隐藏/删除和复制该 div

java - Netbeans (7.3) - 如何在一个项目中创建 'Java Web app' 和 HTML/JavaScript 'HTML 5 Application'?

jquery-ui - jQuery Mobile 弹出窗口和对话框

javascript - 完全删除/删除一个div并显示另一个(切换)

javascript - 使用嵌套元素处理触摸事件?

javascript - 如何知道父节点的子节点号?

javascript - 拉入 JSON 数据

jQuery Accordion : Disable expand on header click, 分配给链接