首页 文章

如何将内部html输入类型的路径和值传递给<form>中主输入类型的路径?

提问于
浏览
1

我的表格中有一个下拉菜单 . 当我在其中选择'other'选项时,会出现一个内部文本框 . 而不是从下拉列表中选择选项,用户更喜欢输入文本 . How can i pass inner html path value(input type=text) entered by the user through path of dropdown menu(input type=select). 我还想要在调用内部html时禁用选择输入 .

这是我的下拉代码

<form:select path="competitorName" name="competitorName" title="Choose a competitor"
                                        onchange="$('#competitor_name').val($.trim($('#myCompetitorId option:selected' ).text()));showfield(this.options[this.selectedIndex].value)"
                                        id="myCompetitorId">
                                        <option value=" ">Please select an competitor</option>
                                        <c:forEach var="competitor" items="${competitors}">
                                            <option
                                                value="<c:out value="${competitor.competitorName}"/>">
                                                <c:out value="${competitor.competitorName}" />
                                            </option>

                                        </c:forEach>
                                        <option value="Other">Other</option>
                                    </form:select>

我的javascript代码

<script type="text/javascript">
function showfield(name){
  if(name=='Other')document.getElementById('div1').innerHTML='<font size="0">Name</font><input type="text" name="competitorName" path="competitorName"/>;
  else document.getElementById('div1').innerHTML='';
}
</script>

1 回答

  • 0

    我不知道你的XML,或者其他什么,但这里有一个可能有用的功能 .

    //<![CDATA[
    function selectToText(selectElement, textElement){
      selectElement.onchange = function(){
        if(textElement.value){
          textElement.value = this.value;
        }
        else{
          textElement.innerHTML = this.value;
        }
      }
    }
    
    var pre = onload, doc, bod, htm, E;
    onload = function(){
    if(pre)pre();
    
    doc = document, bod = doc.body, htm = doc.documentElement;
    E = function(id){
      return doc.getElementById(id);
    }
    selectToText(E('yourSelectId'), E('yourTextElementId'));
    
    }
    //]]>
    

    使用外部JavaScript,将 <script> 标记与 src 中的 src 属性放在一起 . 当然,如果你想使用我的 E 函数,你必须将 'yourSelectId''yourTextElementId' 更改为正确的id .

相关问题