簡體   English   中英

刪除select中每個選項的disabled屬性

[英]remove disabled attribute for each option in select

我有一個選擇與不同的選項。 我設置為在過去30秒內使用的禁用選項。 我想要獲得的是當條件不再有效但頁面仍然打開時再次啟用選項。 以下代碼適用於一個選項,但由於它位於.each()內,因此每個選項都會覆蓋它,但它不起作用。 我正在考慮一個解決方法,存儲diff值,向元素添加一個類,並在該類的項目上運行set timeout,但我認為這種代碼不是最有效的。 有關如何使用更高效的代碼獲得相同結果的任何想法(在每個循環內部可能?)。

$('#destinatario option').each(function(){
    var ds = $(this).attr('data-ts'); 
    console.log("ts vale "+ds);
    var dateArray = ds.split(" ");  // split the date and time 
    var ds1 = dateArray[0].split("-"); // split each parts in date
    var ds2 = dateArray[1].split(":"); // split each parts in time
    var newDate = new Date(ds1[0], (+ds1[1] - 1), ds1[2], ds2[0], ds2[1], ds2[2]).getTime(); //parse it
    console.log("allora vale "+newDate);
    var currentDate = new Date().getTime();
    console.log("adesso vale "+currentDate);
    var diff = currentDate - newDate;
    console.log(diff);
    if(diff < 30000){
        $(this).prop('disabled', true);
        setTimeout(function() {
            $(this).prop('disabled', false);
        }, (30000-diff));
    }
});

嘗試改變最后一部分:

setTimeout(function() {
    $(this).prop('disabled', false);
}.bind(this), (30000-diff));

我有相同的情況,問題我有一個循環,並希望為每個循環綁定一個回調變更,我發現所有這些只是最后一個id工作,並解決它我做了以下綁定參數時調用回調以便保存狀態:

var arr = ["id1" ,"id2", "id3"];
for(i=0; i<arr.length; i++){
  jQuery("#form_target_input").bind('change',function (id, e){
    yiel.fn.setCookie("form_targeting_"+id, true)
   }.bind(null, id));
 }

對於你的情況:

setTimeout(function(element, e) {
   $(element).prop('disabled', false);
}.bind(null, this), (30000-diff));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM