【问题标题】:Function if checkbox checked and a different function if not checked?如果选中复选框,则功能,如果未选中,则使用不同的功能?
【发布时间】:2016-01-31 12:31:05
【问题描述】:

如果 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);
 }
});

【问题讨论】:

  • 不,你不需要两个函数。

标签: javascript jquery three.js


【解决方案1】:

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
    }
});

【讨论】:

    【解决方案2】:

    尝试使用 addEventListener()。这是我首先想到的。也许有更好的解决方案,但我希望它会有所帮助。

    我的想法:

     myCheckbox.addEventListener("change", check);
     function check(){
         if(document.getElementById("myCheck").checked == true){
              this code;
         } else {
              this code;
         }
     }
    

    您也可以在 html 中复选框的 onChange 属性中包含此功能。

    【讨论】:

      猜你喜欢
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多