jQuery H3标签中的jQuery值替换为文本框中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7732664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:12:59  来源:igfitidea点击:

jQuery value in H3 Tag replace by value from Text Box

jquerytextreplace

提问by HaBo

i have a h3 tag

我有一个 h3 标签

<h3> <a href="#"> Initial Text</a></h3>

and i have a TextBox. When a Some values is typed in that TextBox

我有一个文本框。当在该文本框中键入 Some 值时

<input id="Medication" name="Medication" size="50" type="text" value="">

The value in that anchor link (Initial Text)

该锚链接中的值(初始文本)

Should be replaced with the Value in the TextBox.

应替换为 TextBox 中的值。

How can i do that? can any one help me with this?

我怎样才能做到这一点?谁能帮我这个?

Thanks

谢谢

回答by RobB

Try:

尝试:

$('#medication').blur(function(){
  var objVal = $(this).val();
  if(objVal != ''){
    $('h3 a').text(objVal);
  }
});

This will attach the blur()event handler to execute the provided function when the text box loses focus. The function will check if the value is set and if it is then it will update the text in your <a>tag.

这将附加blur()事件处理程序以在文本框失去焦点时执行提供的函数。该函数将检查该值是否已设置,如果已设置,它将更新<a>标签中的文本。

回答by James Allardice

Well it depends on when you want the text in the anchor to change, but it's going to involve binding an event handler to the inputelement. If you want the atext to change whenever a key is pressed, you could bind to the keypressor keyupevent:

好吧,这取决于您希望锚中的文本何时更改,但这将涉及将事件处理程序绑定到input元素。如果您希望在a按下某个键时更改文本,您可以绑定到keypressorkeyup事件:

$("#medication").keyup(function() {
    $("h3 a").text($(this).val());
});

If you want the text to change when the user leaves the input(in other words, on the blurevent), you can bind to the blurevent:

如果您希望文本在用户离开时input(换句话说,在blur事件中)发生变化,您可以绑定到blur事件:

$("#medication").blur(function() {
    $("h3 a").text($(this).val());
});

You may want the text to change when the inputis blurred, but only if the value has changed. If that's the case, bind to the changeevent.

您可能希望文本在input模糊时更改,但前提是值已更改。如果是这种情况,请绑定到change事件。

You may evenwant to bind a handler to more than one of these events, in which case you could use bind, which takes a string representing multiple events:

甚至可能希望将处理程序绑定到多个这些事件,在这种情况下,您可以使用bind,它接受一个表示多个事件的字符串:

$("#medication").bind("keyup blur change", function() {
    $("h3 a").text($(this).val());
});

The important parts of the above code are the $(this).val()which will get the value entered into the inputby the user, and the $("h3 a").text(...)which is what actually changes the text of the anchor.

上述代码的重要部分是$(this).val()获取input用户输入的值,以及$("h3 a").text(...)实际更改锚文本的内容。

回答by John Hartsock

I would first place an ID on the <a>tag or the <h3>tag. This will allow for quick reference to the anchor you are trying to modify and will make the code more efficient.

我会首先在<a>标签或<h3>标签上放置一个 ID 。这将允许快速引用您尝试修改的锚点,并使代码更高效。

<h3> <a id="myAnchor" href="#"> Initial Text</a></h3>

JavaScript/jQuery

JavaScript/jQuery

$(document).ready(function () {
  $('#Medication').change(function () {
    $('#myAnchor').text($(this).val());
  });
});

Note the above makes use of the .change().

注意上面使用了.change()

回答by swatkins

OK, first you bind to the blur event of the input, then, get the value of the text input and replace the link text with that value:

好的,首先绑定到输入的模糊事件,然后,获取文本输入的值并将链接文本替换为该值:

$('#Medication').bind('blur', function(){
     var val = $(this).val(); // get value of input
     $('h3 a').html(val); // replace text of link
});