简体   繁体   中英

jQuery returns false when input empty and press save but when type and click save still returns false

When I click on my hyper link button and modal popup and then press save if input title empty it returns false which is correct, but when I type in the input again and press save it still returns false.

Question If I click on my save button and input#title is empty how to make it return false when is empty but lets me save it when text in input?

Codepen Example // Updated with working code

It's some thing to do with this

if ($.trim(sel) == '') {
    return false;
} else {
    textarea.value = textarea.value.substring(0,start) + replace +
    textarea.value.substring(end,len) + '\n' + id;
    $('#myLink').modal('hide');
}

Full Script

$('#myLink').on('shown.bs.modal', function() {
    var text = getSelectedText();
    $('#title').val(text);
    $('#url').val('http://');
});    

function getSelectedText() {
    var textarea = document.getElementById("message");
    var len = textarea.value.length;
    var start = textarea.selectionStart;
    var end = textarea.selectionEnd;
    var sel = textarea.value.substring(start, end);
    return sel;
}

var counter = 0;

$('#save-link').on('click', function(e) {
    var textarea = document.getElementById("message");
    var len = textarea.value.length;
    var start = textarea.selectionStart;
    var end = textarea.selectionEnd;
    var sel = textarea.value.substring(start, end);

    var replace = '[' + $('input#title').val() + ']' + '[' + counter + ']';

    var id = '\n\n   [' + counter + ']: ' + $('input#url').val();

    counter++;

    if ($.trim(sel) == '') {
        return false;
    } else {
        textarea.value = textarea.value.substring(0,start) + replace +
        textarea.value.substring(end,len) + id;
        $('#myLink').modal('hide');
        $('.alert').remove();
    }
}); 

I have a working solution now. I had to change couple things working code

On this part I add trim()

$('#myLink').on('shown.bs.modal', function() {
    var text = getSelectedText();
    $('#title').val(text.trim());
    $('#url').val('http://');
}); 

And this I added changed to this way

if ($('#title').val().length > 0) {
    textarea.value = textarea.value.substring(0,start) + replace +
    textarea.value.substring(end,len) + '\n' + id;
    $('#myLink').modal('hide');
    $('#myLink form')[0].reset();
} else {

    return false;
}

From

if ($.trim(sel) == '') {
    return false;
} else {
    textarea.value = textarea.value.substring(0,start) + replace +
    textarea.value.substring(end,len) + '\n' + id;
    $('#myLink').modal('hide');
}

Under "save" button's 'click' functionality, check the line

var len = textarea.value.length;

Validate based on the value of the variable len before proceeding further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM