如何用jQuery中另一个文本字段的值替换一个文本字段的一部分? [英] How to replace part of one text field with a value from another text field in jQuery?

查看:127
本文介绍了如何用jQuery中另一个文本字段的值替换一个文本字段的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery根据用户在另一个字段中输入值来动态更新URL(在文本字段中).

I am trying to use jQuery to update a URL (in a text field) dynamically based on the user typing a value in another field.

这是到目前为止我所能获得的: http://jsbin.com/isiqus/10/edit

Here's what I've got so far: http://jsbin.com/isiqus/10/edit

它适用于keyup事件的第一个触发器,但不适用于后续的keyup.谁能告诉我我要去哪里错了(我刚开始使用jQuery).

It works for the first trigger of the keyup event, but not on subsequent keyups. Can anybody tell me where I am going wrong (I'm just getting to grips with jQuery).

推荐答案

您可以尝试:

$(document).ready(function(){
  var valueTemplate = "http://{subdomain}.mydomain.com";
  $("input#alpha").keyup(function(){
    var value = $(this).val();
    $("input#bravo").val(valueTemplate.replace("{subdomain}",value));
   });
});

它将保留一个模板字符串,该模板字符串将在修改文本后呈现另一个输入字段.

It's going to keep a template string, that renders the other input field when the text is modified.

之前发生的是,您在第一个字符串替换中丢失了原始文本模板.

What was happening before was that you were losing your original text template in the first string replace.

这篇关于如何用jQuery中另一个文本字段的值替换一个文本字段的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆