-1

I want to type something in a textfield and click a button,and it paste some words,

just like a paste button ,but always paste the same word after the same textfield ,

this is my code,my opinion is when I click the button ,focus is gone so I use blur, its wrong, anybody know how to do?

$('#but1').click(function(){
    $('#table1 :text').blur(function(){
        var h = 'test'; 
        $(this).val(h+$(this).val());
    });
});

<table id="table1" >
    <tr>
        <td><input type="text" id="text1" size="30"></td>
    </tr>
    <tr>
        <td><input type="text" id="text2" size="30"></td>
    </tr>       
</table>
<input type="button" value="button" id="but1">

sorry ,I have wrong type here,in my jsp the select ID is correct.

4

4 回答 4

1
<input type="button" value="button" id="but1">

您的 id 是but1但在您拥有的 JS 中$('#bu1').click(function(){

更正为$('#but1').click(function(){

您是否试图将#text1' 值放在#text2前面h = 'test'

如果是这样试试这个

$('#but1').click(function () {
    var h = 'test';
    var value = $("#text1").val();
    $("#text2").val(h + value);
});

小提琴

于 2013-07-15T07:13:41.187 回答
1
 $('#bu1').click(function(){

should be

$('#but1').click(function(){

Demo

于 2013-07-15T07:17:10.757 回答
1

Are you trying to do this? FIDDLE

$('#but1').click(function () {
    //$('#table1 :text').blur(function () {
        //var h = 'test';
        //$(this).val(h + $(this).val());
    //});
    var txt = $('#text1').val();
    $('#text2').val(txt);
});

By the way you use $('#bu1'). not the same with your button id.

Just use clicked. Don't make things more complicated.

Cheers :)

于 2013-07-15T07:17:27.457 回答
1

there are few logical errors in your code, i have refined it

JS CODE:

$('#but1').click(function () {
  //$('#table1 :text').blur(function(){ //commented out
  var target = $('#table1 :text'); //added newly
  var newTxt = 'test';
  target.val(newTxt + target.val());
  //}); //commented out
});

JSFIDDLE:

http://jsfiddle.net/KmkTs/

Happy Coding:)

于 2013-07-15T07:24:27.877 回答