如果复选框被选中,则执行一个函数,如果未选中则执行另一个函数?

3

如果ID为m1的单选按钮被选中,我的网格将更新位置。

我尝试让网格在"#m1"不再被选中时返回到原始位置。

如果选中了,则需要触发一个函数,如果未选中,则需要触发另一个函数吗?

<input id="m1" type="radio" name="radio" value="">
<input id="mx" type="radio" name="radio" value="">


var geometry = new THREE.BoxGeometry( 1, 0.05, 1 );
var mesh    = new THREE.Mesh( geometry);
mesh.position.set( 0, 0.012, 0 );
mesh.scale.set( 1, 1, 2 );
mesh.add( plane );


$('#m1').change(function() {
    if(this.checked) {
   var tween = new TWEEN.Tween(mesh.position).to({ x: 0, y: 0.112, z:0 }, 2000).start();
   tween.easing(TWEEN.Easing.Cubic.In);
   tween.yoyo(true);
 }
});

不,你不需要两个函数。 - Script47
2个回答

2

jQuery

$('#m1').on("change", function() {
    if($(this).prop("checked") == true) {
        var tween = new TWEEN.Tween(mesh.position).to({ x: 0, y: 0.112, z:0 }, 2000).start();
        tween.easing(TWEEN.Easing.Cubic.In);
        tween.yoyo(true);
    } else {
        // do another stuff
    }
});

0
尝试使用addEventListener()。这是我首先想到的方法。也许有更好的解决方案,但我希望这个方法会有所帮助。
我的想法:
 myCheckbox.addEventListener("change", check);
 function check(){
     if(document.getElementById("myCheck").checked == true){
          this code;
     } else {
          this code;
     }
 }

你还可以将这个函数包含在HTML复选框的onChange属性中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接