简体   繁体   English

如何避免文本框中的空值

[英]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.

相关问题 如何在HTML文本框中检查null值? - how to check null value in html textbox? 如何在搜索文本框中输入日期值时避免Ajax错误? - How to avoid ajax error while typing a date value in search textbox? 如何定义一个文本框值null = 0并具有值= 1? 用JavaScript - how to define a textbox value null = 0 and has a value = 1? with javascript 当文本框不为null /一些值传递到文本框时,如何从文本框调用javascript函数 - How to call a javascript function from a textbox when the textbox is not null / some value is passed into the textbox 文本框值不为null时如何隐藏按钮 - how to hide button when textbox value isn't null 如何避免在MYSQL / PHP / Javascript中显示具有NULL值的列? - How to avoid displaying the columns that have NULL value in MYSQL/PHP/Javascript? 如何用match()避免“ null” - How to avoid “null” with match() 为dijit / form / TextBox正确设置值时,如何避免触发事件“更改”? - How to avoid firing event 'change' when setting a value properly for a dijit/form/TextBox? 单击按钮时如何避免刷新asp.net中的文本框值? - How to avoid refreshing of textbox value in asp.net when click button? onclick 单选按钮使文本框值 null 并隐藏文本框 - onclick radio button make textbox value null and hide textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM