简体   繁体   English

jQuery将焦点事件替换为文本框值

[英]jQuery replace textbox value with on focusout event

I am trying to adding some text to textbox with on focusout event. 我正在尝试在焦点输出事件中向文本框添加一些文本。

My JS Code is: 我的JS代码是:

jQuery(".search-field input:text").focusout( function() {
    var ckClass = jQuery(this).attr('class').split(' ')[1];

    if(ckClass == 'inp_pa_bedroom'){
        jQuery('.inp_pa_bedroom').val('Hi I am replace');
    }else if(ckClass == 'inp_pa_bathroom'){
        jQuery('.inp_pa_bedroom').val('Hi I am replace');
    }
});

I am applying some condition based on this variable: var ckClass , but value is replacing and disappear. 我正在基于此变量应用一些条件: var ckClass ,但是值正在替换并消失。

I will thankful If someone guide me. 如果有人指导我,我将不胜感激。

jQuery(function($){ // DOM ready and secure $ alias

 $('.search-field').find(".inp_pa_bedroom, .inp_pa_bathroom").focusout(function(){
    this.value = "Hi I am replace";
 });

 // Other DOM ready code here.
 // Use $ instead of jQuery if you care about your keyboard

});

Why not immediately target the desired class elements? 为什么不立即针对所需的class元素呢?
Go to the jQuery API docs and there you can find about the .find() method. 转到jQuery API文档,然后您可以找到有关.find()方法的信息。

try to use hasClass() function. 尝试使用hasClass()函数。 Use ".on" insted of focusout. 使用“ .on”插入焦点。

jQuery(".search-field input:text").on('focusout', function() {    
    if(jQuery(this).hasClass('inp_pa_bedroom')){
        jQuery(this).val('Hi I am replace');
    }else if(jQuery(this).hasClass('inp_pa_bathroom')){
        jQuery(this).val('Hi I am replace');
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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