[英]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. 当我单击超级链接按钮和模式弹出窗口时,如果输入标题为空,则按保存,则返回false,这是正确的; 但是当我再次键入输入并按保存时,它仍返回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?
问题如果我单击保存按钮并且input#title为空,如何使它在为空时返回false,但在输入文本时让我保存呢?
Codepen Example // Updated with working code Codepen示例 //使用工作代码进行了更新
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() 在这一部分,我添加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. 在继续进行之前,请根据变量
len
的值进行验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.