简体   繁体   English

如何在文本框中添加永久词?

[英]How can i add permanent word to the text box?

I am trying to add permanent word to the textbox.in this text box it will take users firstname automatically when user enters the his name in the firstname textbox using onkeyup="copyText()" function.what i want to do is i want s to add permanent word to sitename textbox after getting automatically typed firstname. 我正在尝试向文本框添加永久单词。在此文本框中,当用户使用onkeyup =“ copyText()”函数在名字文本框中输入他的名字时,它将自动使用用户的名字。我要做的是我想要的在自动输入名字后将永久词添加到站点名称文本框中。

Here is my code 这是我的代码

<td><input type="text" id="fname" name="fname"   value=""  placeholder="Your First Name" onkeyup="copyText()"  required /></td>

<td><input type="text" id="sitename" name="sitename" value=""  placeholder="Your Site name" readonly/></td>

Here is my javascript code this is working 这是我的javascript代码,正在工作

<script type="text/javascript">
    function copyText() {
        src = document.getElementById("fname");
        dest = document.getElementById("sitename");
        dest.value = src.value;
    }
</script>

This is pretty doable with jQuery: 这对于jQuery非常可行:

$('#fname').keyup(function(event) {

    event.stopPropagation();

    $('#sitename').val($('#fname').val());
});

You will have to remove your copyText() function for this to work. 您必须删除您的copyText()函数才能正常工作。

Hope this helps. 希望这可以帮助。

I assume you want something like this 我想你想要这样的东西

if a user enters a name for eg 'StackOverflow' 如果用户输入了例如“ StackOverflow”的名称

in the second input box you want to have 'StackOverflow.co.in' where '.co.in' is your permanent text to the text box 在第二个输入框中,您要使用“ StackOverflow.co.in”,其中“ .co.in”是指向文本框的永久文本

However for this you can use simple javascript 但是为此,您可以使用简单的javascript

<input id = "firstName" onkeyup="copyText()" />

<Input id = "siteName" value =".co.in" />

<script type="text/javascript">
function copyText(){
    var firstName = document.getElementById('firstName').value ;
    document.getElementById('siteName').value = firstName + '.co.in' ;
}
</script>

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

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