简体   繁体   English

输入值为空时显示最后一个字符串?

[英]Show last string when input value is empty?

This is my HTML block 这是我的HTML块

<div class="panel-heading">
  <h4 class="panel-title">
    <a data-toggle="collapse" data-parent="#accordion" href="#q1">
     Question 1
    </a>
  </h4>
</div>
<div id="q1" class="panel-collapse collapse">
  <div class="panel-body">
    <div class="form-group">
      <label for="inputQ1" class="col-sm-2 control-label">Question</label>
      <div class="col-sm-7">
        <input type="text" class="form-control" id="inputQ1" placeholder="e.g How was our service?">
      </div>
    </div>
  </div>
</div>

and the Jquery 和jQuery

$('.panel-body input').keyup(function () {
    $(this).closest('.panel-collapse').prev('.panel-heading').find('a').text(this.value);
});

Basically what this does is that whatever the user types in the input box, it will be displayed in the anchor tag. 基本上,这是什么,无论用户在输入框中键入什么内容,它都将显示在锚标记中。

Now, the problem I'm facing is that when the user types something and decides to delete the content in the input box, then the anchor tag will no longer have content. 现在,我面临的问题是,当用户键入内容并决定删除输入框中的内容时,anchor标签将不再包含内容。 Ideally it should always show the previous content whenever the input box is focused. 理想情况下,只要输入框处于焦点状态,它就应始终显示先前的内容。 I hope my description of the problem is clear enough ... 我希望我对问题的描述足够清楚...

Here's a link to JSFiddle http://jsfiddle.net/SpLsZ/ 这是JSFiddle的链接http://jsfiddle.net/SpLsZ/

I have update your fiddle, check: http://jsfiddle.net/SpLsZ/2/ 我已经更新了您的小提琴,请检查: http : //jsfiddle.net/SpLsZ/2/

I put a data variable value in your anchor tag. 我在您的锚标记中放置了一个数据变量value When your text content is changing, I'm now checking if it has a value or not. 当您的文本内容更改时,我现在正在检查它是否具有值。 If it is not, I'm retrieving data value and inserting it as text value. 如果不是,那么我正在检索数据value并将其作为文本值插入。

<a href="#q1" 
    data-toggle="collapse" 
    data-parent="#accordion" 
    data-value="Question 1"
>Question 1</a>
$('.panel-body input').keyup(function () {
   var header = $(this).closest('.panel-collapse').prev('.panel-heading').find('a');

   if(this.value == "") {
      header.text(header.data("value"));
   } else {
      header.text(this.value);
   }
});

Try this: 尝试这个:

$('.panel-body input').keyup(function () {
    $(this).closest('.panel-collapse').prev('.panel-heading').find('a').text(
        this.value || 'Question 1'
    );
});

Working Fiddle 工作小提琴

Working Fiddle: http://jsfiddle.net/SpLsZ/4/ 工作小提琴: http : //jsfiddle.net/SpLsZ/4/

Change you HTML markup like so: 像这样更改您的HTML标记:

<a data-toggle="collapse" data-parent="#accordion" href="#q1">
    <span class="question">Question 1</span>
    <span class="answer"></span>
</a>

This way you don't have to worry about storing any values. 这样,您不必担心存储任何值。 And your JavaScript to: 而您的JavaScript可以:

$('.panel-body input').keyup(function () {
    var $link = $(this).closest('.panel-collapse').prev('.panel-heading').find('a');
    var $question = $link.find('.question');
    var $answer = $link.find('.answer');

    if (this.value) {
        $question.hide();
        $answer.text(this.value);
    } else {
        $question.show();
        $answer.empty();
    }
});

When you have any value you hide the qestion text and show the answer, if no value is entered show question and empty answer. 如果有任何值,则隐藏问题文本并显示答案;如果未输入任何值,则显示问题和空答案。

Try 尝试

$('.panel-body input').keyup(function () {
    $(this).closest('.panel-collapse').prev('.panel-heading').find('a').text(this.value);
}).blur(function () {
    if ($.trim(this.value) == '') {
        $(this).closest('.panel-collapse').prev('.panel-heading').find('a').text(function () {
            return $(this).data('default')
        });
    }
});
$('.panel-heading a').each(function () {
    $(this).data('default', $(this).text());
})

Demo: Fiddle 演示: 小提琴

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

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