创建一个允许用户更改状态的元素 – HTML、CSS 和 JavaScript。

huangapple go评论56阅读模式
英文:

Create an element that allows user to change status - HTML, CSS and JavaScript

问题

我正在创建一个网站上的元素,用户将能够更新工作/订单的状态。

我找到的最好方法是创建一个下拉列表,然后进行样式设置。

HTML

<select name="status" id="status">
    <option value="Completed">已完成</option>
    <option value="Incomplete">未完成</option>
</select>

CSS

#status {
    width: 100px;
    height: 20px;
    background-color: green;
    color: lightgreen;
    font-size: 18px;
    text-align: center;
}

虽然这一切都很好,但我的主要问题是,我希望实际选择框的背景和字体颜色根据所选的选项而改变。即,如果选择了"未完成","background-color"将更改为"橙色","color"将更改为"淡珊瑚色"。

英文:

I am creating an element on a website in which the user will be able to update the status of a job/order.

The best way that I found to make this work is by creating a drop down list and then styling it.

HTML

&lt;select name=&quot;status&quot; id=&quot;status&quot;&gt;
      &lt;option value=&quot;Completed&quot;&gt;Completed&lt;/option&gt;
      &lt;option value=&quot;Incomplete&quot;&gt;Incomplete&lt;/option&gt;
&lt;/select&gt;

CSS

#status {
   width: 100px;
   height: 20px;

   background-color: green;
   color: lightgreen;

   font-size: 18px;
   text-align: center;
}

While this all works well, my main issue is that I would like the background and font colour of the actual select box to change depending on the option selected. I.e, if "Incomplete" was to be selected "background-color" would change to "orange" and "color" would change to "lightcoral".

答案1

得分: 1

这应该可以工作:

option:checked {
 color: lightcoral;
 background-color: orange;
}

编辑
我写成了#status而不是option标签。

注意
这将更改页面上已选择的每个选项,要使其仅在该选择中更改,您必须这样做:

#status > option:checked {
 color: lightcoral;
 background-color: orange;
}
英文:

This should work:

option:checked {
 color: lightcoral;
 background-color: orange;
}

EDIT:
I wrote #status instead of the option tag.

NOTE
This is gonna change EVERY option that is selected on the page, to make it change only in that select you have to do like this:

#status &gt; option:checked {
 color: lightcoral;
 background-color: orange;
}

答案2

得分: 1

你需要js逻辑来根据选定的值更改样式。

示例:

function updateSelect(e) {
  if (e.value == "Incomplete") {
    e.style.background = 'orange';
    e.style.color = 'lightcoral';
  } else {
    e.style.background = 'green';
    e.style.color = 'lightgreen';
  }
}
#status {
  width: 100px;
  height: 20px;

  background-color: green;
  color: lightgreen;

  font-size: 18px;
  text-align: center;
}
<select name="status" id="status" onchange="updateSelect(this)">
  <option value="Completed">Completed</option>
  <option value="Incomplete">Incomplete</option>
</select>
英文:

You need js logic to change style based on selected value.

Example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function updateSelect(e) {
  if (e.value == &quot;Incomplete&quot;) {
    e.style.background = &#39;orange&#39;;
    e.style.color = &#39;lightcoral&#39;;
  } else {
    e.style.background = &#39;green&#39;;
    e.style.color = &#39;lightgreen&#39;;
  }
}

<!-- language: lang-css -->

#status {
   width: 100px;
   height: 20px;

   background-color: green;
   color: lightgreen;

   font-size: 18px;
   text-align: center;
}

<!-- language: lang-html -->

&lt;select name=&quot;status&quot; id=&quot;status&quot; onchange=&quot;updateSelect(this)&quot;&gt;
  &lt;option value=&quot;Completed&quot;&gt;Completed&lt;/option&gt;
  &lt;option value=&quot;Incomplete&quot;&gt;Incomplete&lt;/option&gt;
&lt;/select&gt;

<!-- end snippet -->

答案3

得分: 0

你需要使用一些JavaScript来完成这个任务,可能最简单的方式是为完成和未完成状态添加一个类。以下是一个示例:

它仅在更改值时做出反应,你可以进一步添加一个空状态、预选值,你可以进行自定义...这只是一个如何切换类的示例。

祝好运!

<!-- 开始代码段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->

const status = document.getElementById("status");

status.addEventListener("change", () => {
    if (status.value === "Completed") {
        status.classList.add("completed");
        status.classList.remove("not-completed");
    } else {
        status.classList.add("not-completed");
        status.classList.remove("completed");
    }
});

<!-- 语言: lang-css -->

#status {
   width: 100px;
   height: 20px;

   background-color: green;
   color: lightgreen;

   font-size: 18px;
   text-align: center;
}

#status.completed {
  background-color: blue;
}

#status.not-completed {
  background-color: red;
}


<!-- 语言: lang-html -->

<select name="status" id="status">
      <option value="Completed">Completed</option>
      <option value="Incomplete">Incomplete</option>
</select>

<!-- 结束代码段 -->

希望对你有帮助!

英文:

You have to use a little javascript to do it, maybe the simplest way is to add a class for complete and not complete state, here is an example

It only reacts when you change de value, you could do more adding an empty state, a pre selected value, you can customize it... this is just an example of how to toggle classes.

Good luck!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const status = document.getElementById(&quot;status&quot;);

status.addEventListener(&quot;change&quot;, () =&gt; {
	if (status.value === &quot;Completed&quot;) {
  	status.classList.add(&quot;completed&quot;);
    status.classList.remove(&quot;not-completed&quot;);
  } else {
  	status.classList.add(&quot;not-completed&quot;);
    status.classList.remove(&quot;completed&quot;);
  }
});

<!-- language: lang-css -->

#status {
   width: 100px;
   height: 20px;

   background-color: green;
   color: lightgreen;

   font-size: 18px;
   text-align: center;
}

#status.completed {
  background-color: blue;
}

#status.not-completed {
  background-color: red;
}

<!-- language: lang-html -->

&lt;select name=&quot;status&quot; id=&quot;status&quot;&gt;
      &lt;option value=&quot;Completed&quot;&gt;Completed&lt;/option&gt;
      &lt;option value=&quot;Incomplete&quot;&gt;Incomplete&lt;/option&gt;
&lt;/select&gt;

<!-- end snippet -->

答案4

得分: 0

你需要使用JavaScript监听选择元素上的change事件,并相应地更新样式。

可以尝试类似以下的代码:

<select name="status" id="status">
  <option value="Completed">Completed</option>
  <option value="Incomplete">Incomplete</option>
</select>

CSS部分:

#status {
  background-color: green;
  color: lightgreen;
}

#status.incomplete {
  background-color: orange;
  color: lightcoral;
}

然后是JavaScript部分:

const statusSelect = document.getElementById("status");
statusSelect.addEventListener("change", function() {
  if (this.value === "Incomplete") {
    this.classList.add("incomplete");
  } else {
    this.classList.remove("incomplete");
  }
});
英文:

You will need to use JavaScript to listen for the change event on the select element and update the styling accordingly.

May be try something like this:

&lt;select name=&quot;status&quot; id=&quot;status&quot;&gt;
      &lt;option value=&quot;Completed&quot;&gt;Completed&lt;/option&gt;
      &lt;option value=&quot;Incomplete&quot;&gt;Incomplete&lt;/option&gt;
&lt;/select&gt;

the CSS:

#status {
  background-color: green;
  color: lightgreen;
}

#status.incomplete {
  background-color: orange;
  color: lightcoral;
}

Then the javascript:

const statusSelect = document.getElementById(&quot;status&quot;);
statusSelect.addEventListener(&quot;change&quot;, function() {
  if (this.value === &quot;Incomplete&quot;) {
    this.classList.add(&quot;incomplete&quot;);
  } else {
    this.classList.remove(&quot;incomplete&quot;);
  }
});

答案5

得分: 0

使用JavaScript,将更改事件侦听器附加到下拉菜单并评估值以更改样式是有效的。以下是通过类名预定义CSS样式的一种方法。如果您想要向下拉菜单添加更多选项或下拉菜单样式增加,这也会更加方便。

我还添加了一个没有值的提示选项作为默认样式。可能不是必需的,但作为备选项留了下来。

const statusDropdown = document.querySelector("#status");

statusDropdown.addEventListener("change", (event) => {
  value = statusDropdown.value;
     
  if (value === "Completed") {
    statusDropdown.classList.remove("status-incomplete"); 
    statusDropdown.classList.add("status-complete");
    
  } else if (value === "Incomplete") {
    statusDropdown.classList.remove("status-complete");
    statusDropdown.classList.add("status-incomplete"); 
  } else {
    statusDropdown.className = "";
  }
});
#status {
   width: 100px;
   height: 20px;
   font-size: 18px;
   text-align: center;
}

.status-default {
/*  default styles  */
}

.status-complete {
   background-color: green;
   color: lightgreen;
}

.status-incomplete {
   background-color: orange;
   color: lightcoral;
}
<select name="status" id="status" class="status-default">
      <option value="">Select</option>
      <option value="Completed">Completed</option>
      <option value="Incomplete">Incomplete</option>
</select>

希望这有助于您的项目!

英文:

Using Javascript, attaching a change event listener to the dropdown and evaluating the value to change styling will work. Below is a way of pre-defining the CSS styles by class names. This would also make it easier if you wanted to add more options to the dropdown or if the dropdown style grows.

I also added a prompt option with no value as a default style. Needed, probably not but left it in as an alternative.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const statusDropdown = document.querySelector(&quot;#status&quot;);

statusDropdown.addEventListener(&quot;change&quot;, (event) =&gt; {
  value = statusDropdown.value;
     
  if (value === &quot;Completed&quot;) {
    statusDropdown.classList.remove(&quot;status-incomplete&quot;); 
    statusDropdown.classList.add(&quot;status-complete&quot;);
    
  } else if (value === &quot;Incomplete&quot;) {
    statusDropdown.classList.remove(&quot;status-complete&quot;);
    statusDropdown.classList.add(&quot;status-incomplete&quot;); 
  } else {
    statusDropdown.className = &quot;&quot;;
  }
});

<!-- language: lang-css -->

#status {
   width: 100px;
   height: 20px;
   font-size: 18px;
   text-align: center;
}

.status-default {
/*  default styles  */
}

.status-complete {
   background-color: green;
   color: lightgreen;
}

.status-incomplete {
   background-color: orange;
   color: lightcoral;
}

<!-- language: lang-html -->

&lt;select name=&quot;status&quot; id=&quot;status&quot; class=&quot;status-default&quot;&gt;
      &lt;option value=&quot;&quot;&gt;Select&lt;/option&gt;
      &lt;option value=&quot;Completed&quot;&gt;Completed&lt;/option&gt;
      &lt;option value=&quot;Incomplete&quot;&gt;Incomplete&lt;/option&gt;
&lt;/select&gt;

<!-- end snippet -->

答案6

得分: 0

使用纯CSS,您可以使用has():checked来样式化它。

select {
  background-color: red;
}

select:has(option[value="Completed"]:checked) {
  background-color: green;
  color: yellow;
}
<select name="status" id="status">
  <option value="Completed">Completed</option>
  <option value="Incomplete">Incomplete</option>
</select>
英文:

With just CSS you can style it with has() and :checked

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

select {
  background-color: red;
}

select:has(option[value=&quot;Completed&quot;]:checked) {
  background-color: green;
  color: yellow;
}

<!-- language: lang-html -->

&lt;select name=&quot;status&quot; id=&quot;status&quot;&gt;
      &lt;option value=&quot;Completed&quot;&gt;Completed&lt;/option&gt;
      &lt;option value=&quot;Incomplete&quot;&gt;Incomplete&lt;/option&gt;
&lt;/select&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月10日 23:27:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定