jquery将值附加到输入字段

时间:2013-04-25 13:46:33

标签: jquery html css3

我正在尝试使用jquery动态添加到我的输入字段值。但传递的值不会附加到目标。我的代码看起来如下

//on click popup modal with a form
    $(document).on('click', '#form', function(e) {
        e.preventDefault();         

        $('<div></div>').load($(this).attr('href')).dialog({
            title: $(this).attr('title'),
            //autoOpen: false,
            modal: true,            
            draggable: true,
            width:900,
            position: ['top',50], 
            close: function(event, ui) 
            { 
                $(this).dialog('destroy').remove()
            } 
        });
//this value should be appended to one of the input field, the value exists on alert but is not getting appended
        $('input#inputPrepend cashback').val( $(this).closest('tr').children('td.cashback').text() );

    });
  

注意:

//this value is picked up from my Jquery Datatable clicked row
$(this).closest('tr').children('td.cashback').text()
//this input field is getting created inside of modal
$('input#inputPrepend cashback')

HTML

 <div class="formRow fluid">    
      <span class="grid12">
      <span class="grid2"><label class="form">Cashback</label></span>
      <span class="grid3">
        <div class="input-prepend">
           <span class="add-on">€</span>
              <input style="width:120px;height:30px; position relative; top:-10px"type="text"  id="inputPrepend cashback" type="text"  readonly="readonly" value="" />
        </div>
    </span>
    </span>            
    </div>

1 个答案:

答案 0 :(得分:1)

在你的例子中:

$('input#inputPrepend cashback').val( $(this).closest('tr').children('td.cashback').text() );

$(this)是指文档,而不是$('input#inputPrepend cashback')

您可以改用它:

var $input =  $('input#inputPrepend cashback');
$input.val( $input.closest('tr').children('td.cashback').text() );