select标签默认选择选项

 商品管理

 商品管理效果图首页

 

当点击编辑的时候,该数据类型(坚果)跳转到编辑页面,select标签默认选择该类型

 

 

 

body里的内容

<h1>编辑商品</h1>
<form action="/product/updateProduct">
    <input type="hidden" name="id" value="${product.id}" /> <br>
    姓名:<input type="text" name="name" value="${product.name}" /><br>
    类型:<select id="pid" name="pid">
            <c:forEach items="${protype}" var="protype">
                <option value="${protype.pid}">${protype.tname}</option>
            </c:forEach>
        </select><br>
    价格:<input type="text" name="price" value="${product.price}" /><br>
    图片:<input type="text" name="picture" value="${product.picture}" /><br>
    <p>
        <input type="submit" value="更新"/>
    </p>
</form>

 javascript默认选择select标签中的option

 

function selectoption(){
        var num = ${product.pid};               //获取input中输入的数字
        var numbers = $("#pid").find("option"); //获取select下拉框的所有值
        for (var j = 1; j < numbers.length; j++) {
            if ($(numbers[j]).val() == num) {
                $(numbers[j]).attr("selected", "selected");
            }
        }
    }
    selectoption();

 

 

每天进步一小步,继续努力!

  

 

posted @ 2018-12-04 13:56  言简义丰  阅读(3769)  评论(0编辑  收藏  举报