最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jsp attribute DISABLE by condition - Stack Overflow

programmeradmin4浏览0评论

On this time I have a list of objects Product with attributes name, a list of Category and quantity.

In my jsp tag i have the following select:

<select name="quantity" onchange="this.form.submit()">

This select is used to change de product quantity and send the new value to the back end. But I have one case where i need to disable the select

<select name="quantity" onchange="this.form.submit()" disabled="true">

How can I enable or disable the select depending on the category string value?

On this time I have a list of objects Product with attributes name, a list of Category and quantity.

In my jsp tag i have the following select:

<select name="quantity" onchange="this.form.submit()">

This select is used to change de product quantity and send the new value to the back end. But I have one case where i need to disable the select

<select name="quantity" onchange="this.form.submit()" disabled="true">

How can I enable or disable the select depending on the category string value?

Share Improve this question edited Sep 1, 2017 at 16:49 Akshay Pethani 2,6002 gold badges32 silver badges44 bronze badges asked Aug 31, 2017 at 18:02 Martin LarizzateMartin Larizzate 9103 gold badges14 silver badges32 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

If you're using Jquery, you can do this to disable a select. I added an id to make it easier.

add id attribute:

<select name="quantity" id="quantity" onchange="this.form.submit()">

disable via jquery:

$('#quantity').prop('disabled', 'disabled');

You can also disable it via JSTL if you're using that. Previous answer mentions choose,when,otherwise tags but something like this can be done with just a simple if tag.

if quantity request variable set by servlet is empty

<c:if test="${empty quantityVar}">
<select name="quantity" onchange="this.form.submit()" disabled="true">
</c:if>

if quantity request variable set by servlet is not empty

<c:if test="${not empty quantityVar}">
<select name="quantity" onchange="this.form.submit()">
</c:if>

You can use the JSTL core tag <c:choose>.

The <c: choose> works like a Java switch statement in that it lets you choose between a number of alternatives. Where the switch statement has case statements, the <c:choose> tag has <c:when> tags. Just as a switch statement has the default clause to specify a default action, <c:choose> has <c:otherwise> as the default clause.

Example:

<%@ taglib uri = "http://java.sun./jsp/jstl/core" prefix = "c" %>
<html>
   <head>
      <title><c:choose> Tag Example</title>
   </head>
   <body>
      <c:set var = "salary" scope = "session" value = "${2000*2}"/>
      <p>Your salary is : <c:out value = "${salary}"/></p>
      <c:choose>

         <c:when test = "${salary <= 0}">
            Salary is very low to survive.
         </c:when>

         <c:when test = "${salary > 1000}">
            Salary is very good.
         </c:when>

         <c:otherwise>
            No ment sir...
         </c:otherwise>
      </c:choose>

   </body>
</html>

You could just write a function in JS that disables the second select depending on what your first select oute is.

It may be best to start with some basics from the following link:

https://www.w3schools./js/tryit.asp?filename=try_dom_select_disabled

发布评论

评论列表(0)

  1. 暂无评论