Html列表添加文本框

时间:2012-11-16 16:10:42

标签: javascript jquery html css cordova

我需要你的帮助我想在每次添加一个成分到列表时放一个小文本框。就在旁边,所以我可以把数量。有人可以帮帮我吗? 这是html:

<html>
    <head>
    <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="themes/receta.css" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
    <script src="js/recetas.js">  </script>  

<body>
    <label for="receta">Nombre Receta:</label><br>
    <input type="text" name="receta" value="" maxlength="20" /><br>

<h1>INGREDIENTES</h1>
<ul>

</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>
<button>GUARDAR</button>    
<li><a href="resumen.html" data-transition="none" rel="external">testing2</a></li>
</body>
</html>

这是我不知道如何更改的js,所以我可以放置文本框:

    function selectIngredient(select)
        { 
          var $ul = $(select).closest('.ui-select').prev('ul');
    console.log($ul[0])
      if ($ul.find('input[value=' + $(select).val() + ']').length == 0) {
        console.log('s')
            $ul.append('<li onclick="$(this).remove();">' +
              '<input type="hidden" name="ingredients[]" value="' + 
              $(select).val() + '" /> ' +
              $(select).find('option:selected').text() + '</li>');
      }
        }

2 个答案:

答案 0 :(得分:1)

我不知道我是否理解正确,但我在你的代码中看到的是隐藏的输入和列表中的删除。我删除了隐藏的属性,并在一个范围上放置了成分的名称以设置删除功能,我将一个id设置为名为ingredientes的ul。

以下是代码:

$("#addIng").change(function(){
      var $ul = $("#ingredientes");
console.log($ul[0])
  if ($ul.find('input[value=' + $(this).val() + ']').length == 0) {
    console.log('s')
        $ul.append('<li>' +
          '<input name="ingredients[]" value="' + 
          $(this).val() + '" /> <span onclick="$(this).parent().remove();">' +
          $(this).find('option:selected').text() + '</span></li>');
      }

});

jsfiddle

答案 1 :(得分:0)

你的html存在一些问题,这使得它很奇怪但只是回答你的问题并帮助你继续努力。

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

这应该可行,但是你应该为你的列表提供一个id或类,这样就更容易找到它,这段代码会在整个页面的每个列表中添加一个项目。

我希望无论如何它会帮助你。