[英]How to avoid null value in textbox
$(document).on("click", "label.radeem-textbox", function () { var txt = $(".mytxt").text(); $(".radeem-textbox").replaceWith("<input class='radeem-textbox redeem-textbox'/>"); $(".radeem-textbox").val(txt); return false; }); $(document).on("blur", "input.radeem-textbox", function () { var txt = $(this).val(); $(this).replaceWith("<label class='radeem-textbox'></label>"); $(".radeem-textbox").text(txt); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <a href="javascript:void(0)" class="right-side-margin-top right-side-margin-bottom display-block add-to-wishlist"> <label class="add-to-wish radeem-textbox">Redeem a Coupon</label> </a>
I have jquery code for label convert to textbox on click. 我有用于将标签单击转换为文本框的jQuery代码。 But when i not enter nothing on textbox, the textbox and label is not showing.. So how to make textbox focused if not enter nothing..
但是,当我在文本框上未输入任何内容时,该文本框和标签未显示..因此,如果不输入任何内容,如何使文本框成为焦点。
<label class="add-to-wish radeem-textbox">Redeem a Coupon</label>
<script type="text/javascript">
$(document).on("click", "label.radeem-textbox", function () {
var txt = $(".mytxt").text();
$(".radeem-textbox").replaceWith("<input class='radeem-textbox'/>");
$(".radeem-textbox").val(txt);
return false;
});
$(document).on("blur", "input.radeem-textbox", function () {
var txt = $(this).val();
$(this).replaceWith("<label class='radeem-textbox'></label>");
$(".radeem-textbox").text(txt);
});
Add with if(txt.trim())
. 添加
if(txt.trim())
。 trim() It will validate the input box is empty or not.Its only allowed its not empty,null
. trim()将验证输入框是否为空。只允许其为非
empty,null
。
$(document).on("click", "label.radeem-textbox", function () { var txt = $(".mytxt").text(); $(".radeem-textbox").replaceWith("<input class='radeem-textbox'/>"); $(".radeem-textbox").val(txt); return false; }); $(document).on("blur", "input.radeem-textbox", function () { var txt = $(this).val(); if(txt.trim()){ $(this).replaceWith("<label class='radeem-textbox'></label>"); $(".radeem-textbox").text(txt); } else{ console.log('enter text please') } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label class="add-to-wish radeem-textbox">Redeem a Coupon</label>
Alternative way is : update with label tag contenteditable its same as your thing 另一种方法是: 与标签标签更新 CONTENTEDITABLE它同你的事
<label contenteditable="true">Click me to edit me</label>
$(document).on("blur", "input.radeem-textbox", function () {
var txt = $(this).val();
if($.trim(txt).length) { // if not empty value
$(this).replaceWith("<label class='radeem-textbox'></label>");
$(".radeem-textbox").text(txt);
} else {
//empty value so focus again.
$(this).focus();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.