开发者

Why do `keyup` and `if` not work in my code?

I want if values into input was equal with one of the words ed Or d Or i Or ei done perform the operation it. i can not change value input value="ed" in first run page. what do i do?

Example: http://jsfiddle.net/ZsYdY/3/

<input type="text" value="ed">
<div class="code"></div>


function ticket_check() {
    //var val = 'ticket_code=' + $('.ticket_code').val();
    var input_val = $('input').val();
    if (input_val == 'ed Or d Or i O开发者_如何转开发r ei') {
        $('.code').empty().hide().fadeIn('slow').append('It is ok');
    } else {

            $('.code').fadeOut('slow', function () {
                $(this).hide().fadeIn('slow').append('It is not ok');
            })

    }
}

$('.input').live('keyup change', function () {
    ticket_check();
    var input_val = $('input').val();
    if (input_val == 'ed Or d Or i Or ei') {
        $('.code').fadeOut('slow', function () {
            $(this).hide().fadeIn('slow').append('It is not oooooooookkkkkkkkk');
        })
    }
});
ticket_check()


if (input_val == 'ed Or d Or i Or ei') {

needs to be:

if (input_val == 'ed' || input_val == 'd' || input_val == 'i' || input_val == 'ei') {

And $('.input') should be $('input') in your example

Updated Example


Use the following code to check whether the input equals one of the given suggestions:

//Case-sensitive
'ed d i ei'.split(' ').indexOf(input_val) != -1

//Alternatively, case-sensitive:
['ed', 'd', 'i', 'ei'].indexOf(input_val) != -1;

//Case-insensitive:
'ed d i ei'.split(' ').indexOf(input_val.toLowerCase()) != -1

The concept behind this example is: "Split a string in a list of valid values and check whether the current value occurs". Example:

function ticket_check() {
    //var val = 'ticket_code=' + $('.ticket_code').val();
    var valid_values = 'ed d i ei'.split(' ');
    var input_val = $('input').val().toLowerCase();
    if (valid_values.indexOf(input_val) != -1) {
        //Rest of code


Two things.... your input doesn't have the class '.input' on it. So this line should look like this

$('input').live('keyup change', function () { 

Also, as others have stated... your or is messed up. Look at one of the above answers for that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜