从元素中删除 Jquery 并重新应用?

Remove Jquery from element and reapply?

我正在使用 wickedpicker 并且有一个包含 2 个时间字段的页面。

我需要将每个字段中的时间分别更新为另一个字段,因为按下了一个按钮。

我尝试过的所有方法都不起作用,我认为这是因为脚本已经绑定到输入元素(不确定这是不是正确的术语)。

所以我的问题是加载和绑定的脚本是否可以卸载,然后使用不同的参数重新应用?

这是我目前拥有的代码:

<script>
      $(document).ready(function() {
            $(".timepicker").wickedpicker({twentyFour: true});


            $("#btn").click(function(e) {
                alert ('click')
                $("#timeTwo").wickedpicker({twentyFour: true, now: "17:35"});
            })
      });
    </script>

    <body>

    <button id='btn' class='formBtn'>CHANGE</button>

    <input type='text' class='timepicker' id='timeOne' name='timeOne' value='' >

    <input type='text' class='timepicker' id='timeTwo' name='timeTwo' value='' >

    </body>

我最初正在加载时间选择器,因为我需要能够立即使用它,但我还需要能够通过单击按钮更新时间。

我试过在 timeTwo 中设置一个默认值,它被时间选择器覆盖了。我已经使用 JQuery 设置了 timeTwo 的值并且输入显示了正确的值,但是时间选择器显示的是现在的时间,而不是字段显示的时间。

有什么想法吗? 谢谢

更新 删除和读取元素有效。

$("#btn").click(function(e) {
     console.log ('click')
     var test = $("#timeTwo").remove();
     $('body').append( test ); // later
    $("#timeTwo").wickedpicker({twentyFour: true, now: '15:34'});
});

但是我如何确保它被加载回相同的位置? 谢谢

你可以按照下面的方法做,你可以在初始化后动态更改选项..参考document

$(document).ready(function() {
     $(".timepicker").wickedpicker({twentyFour: true});
     $("#btn").click(function(e) {
         alert ('click')
         $("#timeTwo").wickedpicker('twentyFour', true);
         $("#timeTwo").wickedpicker('now', "17:35");
     });
});

这里的 wickedpicker: https://ericjgagnon.github.io/wickedpicker/ 没有设置内部使用时间的方法。当它检测一个元素是否已经有一个 wickedpicker 时,您不能像您已经指出的那样简单地第二次应用它。 最干净的解决方案是使用所需的功能扩展 wickedpicker。另一种解决方法可能是删除输入并重新创建它。

编辑:我给你做了一个小片段来演示选择器的替换。这不是一个很好的解决方案,因为它会破坏其他事件或引用 on/to 日期选择器元素。你必须小心这些。

扩展原来的 wickedpicker 也不是很难,但我认为超出了这个问题。

PS:请忽略css部分。我必须内联 wickedpicker css 才能让它工作(减去正确的字体)。您应该只需要 javascript 部分。

 $(document).ready(function() {
   $(".timepicker").wickedpicker({
     twentyFour: true
   });


   $("#btn").click(function(e) {
     $("#timeTwo").replaceWith("<input type='text' class='timepicker' id='timeTwo' name='timeTwo' value='' />");
     $("#timeTwo").wickedpicker({
       twentyFour: true,
       now: "17:35"
     });
   })
 });
.wickedpicker {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  box-shadow: 0 0 0 1px rgba(14, 41, 57, .12), 0 2px 5px rgba(14, 41, 57, .44), inset 0 -1px 2px rgba(14, 41, 57, .15);
  background: #fefefe;
  margin: 0 auto;
  border-radius: .1px;
  width: 270px;
  height: 130px;
  font-size: 14px;
  display: none
}
.wickedpicker__title {
  background-image: -webkit-linear-gradient(top, #fff 0, #f2f2f2 100%);
  position: relative;
  background: #f2f2f2;
  margin: 0 auto;
  border-bottom: 1px solid #e5e5e5;
  padding: 12px 11px 10px 15px;
  color: #4C4C4C;
  font-size: inherit
}
.wickedpicker__close {
  -webkit-transform: translateY(-25%);
  -moz-transform: translateY(-25%);
  -ms-transform: translateY(-25%);
  -o-transform: translateY(-25%);
  transform: translateY(-25%);
  position: absolute;
  top: 25%;
  right: 10px;
  color: #34495e;
  cursor: pointer
}
.wickedpicker__close:before {
  content: '[=11=]d7'
}
.wickedpicker__controls {
  padding: 10px 0;
  line-height: normal;
  margin: 0
}
.wickedpicker__controls__control,
.wickedpicker__controls__control--separator {
  vertical-align: middle;
  display: inline-block;
  font-size: inherit;
  margin: 0 auto;
  width: 35px;
  letter-spacing: 1.3px
}
.wickedpicker__controls__control-down,
.wickedpicker__controls__control-up {
  color: #34495e;
  position: relative;
  display: block;
  margin: 3px auto;
  font-size: 18px;
  cursor: pointer
}
.wickedpicker__controls__control-up:before {
  content: '\e800'
}
.wickedpicker__controls__control-down:after {
  content: '\e801'
}
.wickedpicker__controls__control--separator {
  width: 5px
}
.text-center,
.wickedpicker__controls,
.wickedpicker__controls__control,
.wickedpicker__controls__control--separator,
.wickedpicker__controls__control-down,
.wickedpicker__controls__control-up,
.wickedpicker__title {
  text-align: center
}
.hover-state {
  color: #3498db
}
@font-face{font-family:fontello;src:url(../fonts/fontello.eot?52602240);src:url(../fonts/fontello.eot?52602240#iefix) format("embedded-opentype"),
url(../fonts/fontello.woff?52602240) format("woff"),
url(../fonts/fontello.ttf?52602240) format("truetype"),
url(../fonts/fontello.svg?52602240#fontello) format("svg");
font-weight:400;
font-style:normal
}
.fontello-after:after,
.fontello:before,
.wickedpicker__controls__control-down:after,
.wickedpicker__controls__control-up:before {
  font-family: fontello;
  font-style: normal;
  font-weight: 400;
  speak: none;
  display: inline-block;
  text-decoration: inherit;
  width: 1em;
  margin-right: .2em;
  text-align: center;
  font-variant: normal;
  text-transform: none;
  line-height: 1em;
  margin-left: .2em;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale
}
.clearable-picker {
  position: relative;
  display: inline-block
}
.clearable-picker>.hasWickedpicker {
  padding-right: 1em
}
.clearable-picker>.hasWickedpicker::-ms-clear {
  display: none
}
.clearable-picker>[data-clear-picker] {
  position: absolute;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
  font-weight: 700;
  font-size: .8em;
  padding: 0 .3em .2em;
  line-height: 1;
  color: #bababa;
  cursor: pointer
}
.clearable-picker>[data-clear-picker]:hover {
  color: #a1a1a1
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://raw.githubusercontent.com/ericjgagnon/wickedpicker/master/dist/wickedpicker.min.js"></script>

<button id='btn' class='formBtn'>CHANGE</button>
<input type='text' class='timepicker' id='timeOne' name='timeOne' value='' />
<input type='text' class='timepicker' id='timeTwo' name='timeTwo' value='' />