2013-07-19 22 views

回答

1

使用.val()得到輸入的值,並將其與一個字符串

var str = $('#datapicker').val(), // jQuery 

    // str = document.getDocumentByI('datapicker').value (vanilla js) 

    strToCompare = '01/07/2013'; 

if(str === strToCompare) { 

    // do something 
} 

比較和在任一change或任何keyup事件調用它包住這個..

$('#datepicker').change(function() { 

    // code goes here 
}); 

更新

試試下面的代碼。

$(function() { 
    var $datepicker = $('#datepicker'); 
    $datepicker.datepicker(); 
    $datepicker.on('change', function() { 
     var str = $datepicker.val(), 
      strToCompare = '07/19/2013'; 

     if (str === strToCompare) { 

      console.log('Strings match') 
     } 
     else { 
      console.log('boom !!') 
     } 
    }); 
}); 

Check Fiddle

+0

對不起的人,也沒有工作。我在(做某事)部分添加了document.write,並沒有顯示任何文本。 – user2379090

+0

你可以顯示你寫的代碼..避免使用'document.write' ..嘗試使用console.log –

+0

allardadam.me.uk/javascript - 我剛剛上傳 – user2379090

0

您輸入有2個IDS。您需要刪除id="food"。那麼下面應該與IE瀏覽器> = 9:

document.getElementById('datepicker').addEventListener(
    'input', 
    function(event) { 
    if (event.target.value.match(/^\d+\/\d+\/\d+$/)) 
     console.log("Hello"); 
    }, false); 
1

可以在屬性onchange

onchange="if(this.value == 'someValue') alert('...');" 

插入代碼或創建新的功能

function change(element){ 
    if(element.value == 'someValue'){ 
     alert('...'); 
    } 
} 

並添加屬性

onchange="change(this);" 

或者添加事件

var el = document.getElementById('input-id'); 
el.onchange = function(){ 
    change(el); // if 'el' doesn't work, use 'this' instead 
} 

我不知道,如果它的工作原理,但它應該:)

相關問題