【问题标题】:getting id from datalist从数据列表中获取 id
【发布时间】:2016-01-26 18:47:56
【问题描述】:

我需要帮助,因为我需要转换它:

<select name="productId">
       <option value=""></option>
       <c:forEach items="${productsBeanId.activeProductsList}" var="product">                    
                <option value="${product.productId}">${product.productName}</option>
       </c:forEach>
 </select>

关于它:

<input list="productId">
      <datalist id="productId">
          <c:forEach items="${productsBeanId.activeProductsList}" var="product">                    
              <option value="${product.productId}">${product.productName}</option>
          </c:forEach>                  
      </datalist>

我需要让 productId 在数据库上使用它,创建一个产品。

&lt;option value="${product.productId}"&gt;${product.productName}&lt;/option&gt; 这行对我来说是个问题,因为我需要保存 productId,但要在列表中显示 productName,以便于查找。

你能帮帮我吗?

【问题讨论】:

    标签: javascript html input datalist


    【解决方案1】:

    试试这个

    var selectObj = document.getElementById( "productId" );
    var innerHTMLSelect = selectObj.innerHTML; //first save the innerHTML of select 
    var selectParent = selectObj.parentNode();
    selectParent.removeNode(selectObj); //remove current select element
    
    selectParent += '<input list="productId"><datalist id="productId"></datalist>'; //append the data list to the parent
    document.getElementById( "productId" ).innerHTML = innerHTMLSelect ; //put the saved innerHTML to the current data list element
    

    【讨论】:

    • 您好:这种方式对我来说是不正确的,因为使用这种解决方案您正在混合概念。
    • @FranGV 没找到你。这不是转换为您想要的输出吗?
    • 不,我解决了它从数据库中按名称获取所需的产品,但 id 更好地获取整个产品。目前是通过这种方式解决的。
    猜你喜欢
    • 1970-01-01
    • 2014-04-01
    • 2018-11-07
    • 2011-09-10
    • 2011-01-23
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多