如何使图像的行为类似复选框,并将其值发送到后端

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

How to make image behave like a checkbox and send its value to backend

问题

<table style="width:70%">
<tr>
<th>
<center>0</center>
<img src="images/chairgreen.png" id="1" style="width:64px;height:64px;" onclick="changeImage(this)">
</th>
</tr>
</table>

<script>
function changeImage(imageElement) {
    if (imageElement.src.endsWith('chairgreen.png')) {
        imageElement.src = 'images/chairorange.png';
    } else if (imageElement.src.endsWith('chairorange.png')) {
        imageElement.src = 'images/chairgreen.png';
    }
}
</script>

问题在于onclick方法,但我不知道如何修复它。我也不知道如何将椅子的ID和颜色发送到后端。对于后端,我将使用Java Spring。


<details>
<summary>英文:</summary>

I want to make an image like a checkbox. (I.e. The picture is a green chair icon. When you press it, it should turn from green to orange, and when pressed again, orange to green.)

The following code works partially, when you click on the green icon, it turns orange, but if I click again to go from orange to green, nothing happens and it stays orange again

The image:

![][1]
  

<table style="width:70%">
<tr>
<th>
<center>0</center>
<img src="images/chairgreen.png" id="1" style="width:64px;height:64px;" onclick="if (src='images/chairgreen.png') { this.src='images/chairorange.png' } else if (src='images/chairorange.png') { this.src='images/chairgreen.png'}" >
</th>
</tr>


The problem is the `onclick` method but I don&#39;t know how to fix it. And I also don&#39;t know how to send the id and the color of the chair to the backend. For the backend I will use java spring.

  [1]: https://i.stack.imgur.com/7jVmM.png

</details>


# 答案1
**得分**: 1

你可以尝试将类名定义为图像的颜色。然后,你可以根据类名更改src属性。不要忘记在每次点击时更改类名。

```javascript
function changeImg(img){
    if(img.classList.contains("green")){
        img.setAttribute("src","images/chairorange.png");
        img.classList.remove("green");
        img.classList.add("orange");
    } 
    else if(img.classList.contains("orange")){
        img.setAttribute("src","images/chairgreen.png");
        img.classList.remove("orange");
        img.classList.add("green");
    }
}
<img src="images/chairgreen.png" id="1" class="green"  style="width:64px;height:64px;" onclick="changeImg(this)">
英文:

You can try to define class name as color of image. Then you can change the src attribute according to class name. Don't forget to change class name on every click.

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

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

function changeImg(img){
if(img.classList.contains(&quot;green&quot;)){
img.setAttribute(&quot;src&quot;,&quot;images/chairorange.png&quot;);
img.classList.remove(&quot;green&quot;);
img.classList.add(&quot;orange&quot;);
} 
else if(img.classList.contains(&quot;orange&quot;)){
img.setAttribute(&quot;src&quot;,&quot;images/chairgreen.png&quot;);
img.classList.remove(&quot;orange&quot;);
img.classList.add(&quot;green&quot;);
}
}

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

&lt;img src=&quot;images/chairgreen.png&quot; id=&quot;1&quot; class=&quot;green&quot;  style=&quot;width:64px;height:64px;&quot; onclick=&quot;changeImg(this)&quot;&gt;

<!-- end snippet -->

答案2

得分: 0

不要在附加点击处理程序时定义函数并运行多个语句。
如果您有一个复杂的操作,编写一个函数,然后触发该函数。

&lt;img src=&quot;images/chairgreen.png&quot; id=&quot;1&quot; data-src=&quot;green&quot; style=&quot;width:64px;height:64px;&quot; onclick=&quot;change();&quot; &gt;

&lt;script&gt;
function change(e) {
    const target = e.target;
    const current = target.getAttribute(&#39;data-src&#39;);
    switch(current) {
        case &quot;green&quot;:
            target.src = &quot;images/chairorange.png&quot;;
            target.setAttribute(&#39;data-src&#39;, &quot;orange&quot;);
            break;
        case &quot;orange&quot;:
            target.src = &quot;images/chairgreen.png&quot;;
            target.setAttribute(&#39;data-src&#39;, &quot;green&quot;);
            break;
        default:
            console.error(&quot;error&quot;, e, current);
            break;
    } 
}
&lt;/script&gt;

但是,如果您在脚本标记中编写代码,最好也在 JavaScript 中生成按钮:

const img = document.createElement(&#39;img&#39;);
img.setAttribute(&#39;src&#39;, &quot;images/chairgreen.png&quot;)
img.setAttribute(&#39;id&#39;, &quot;1&quot;) // 使用迭代器
img.setAttribute(&#39;data-src&#39;, &quot;green&quot;);
img.style.height = &quot;64px&quot;;
img.style.width = &quot;64px&quot;;
img.addEventListener(&#39;click&#39;, change);

如果通过 JavaScript 生成元素,您还需要将其附加到 DOM 中:

const body = document.querySelector(&#39;body&#39;);
body.appendChild(img);

注意:上述内容是您提供的代码段的翻译,没有进行逐字翻译。

英文:

don't define functions and run multiple statements when you attach an on click handler.
if you have a complicated thing, write a function and then trigger that one function.

&lt;img src=&quot;images/chairgreen.png&quot; id=&quot;1&quot;  data-src=&quot;green&quot; style=&quot;width:64px;height:64px;&quot; onclick=&quot;change();&quot; &gt;

&lt;script&gt;
function change(e){
    const target = e.target;
    const current = target.getAttribute(&#39;data-src&#39;);
    switch(current){
      case &quot;green&quot;:
        target.src= &quot;images/chairorange.png&quot;;
        target.setAttribute(&#39;data-src&#39;,&quot;orange&quot;);
          break;
        case &quot;orange&quot;:
        target.src= &quot;images/chairgreen.png&quot;;
        target.setAttribute(&#39;data-src&#39;,&quot;green&quot;);
          break;
        default:
          console.error(&quot;error&quot;, e, current);
            break;
    } 
}

&lt;/script&gt;

but if you are writing in a script tag, might as well generate the button in js as well

const img = document.createElement(&#39;img&#39;);
  img.setAttribute(&#39;src&#39;, &quot;images/chairgreen.png&quot;)
  img.setAttribute(&#39;id&#39;, &quot;1&quot;) // use an iterator
  img.setAttribute(&#39;data-src&#39;, &quot;green&quot;);
  img.style.height =&quot;64px&quot;;
  img.style.width = &quot;64px&quot;;
  img.addEventListener(&#39;click&#39;, change);

you also would need to append to the DOM if you generate elements via js.


const body = document.querySelector(&#39;body&#39;);
body.appendChild(img);

答案3

得分: 0

你正在使用只有一个等号的 if 比较,你至少需要两个等号来进行比较。

onclick=&quot;this.src = (this.src == &#39;images/chairgreen.png&#39;) ? &#39;images/chairorange.png&#39; : &#39;images/chairgreen.png&#39;&quot;
英文:

you are doing the comparison if with only 1 equal sign, you need at least 2 to compare.

onclick=&quot;this.src = (this.src == &#39;images/chairgreen.png&#39;) ? &#39;images/chairorange.png&#39; : &#39;images/chairgreen.png&#39;&quot;

huangapple
  • 本文由 发表于 2020年8月27日 22:18:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63618046.html
匿名

发表评论

匿名网友

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

确定