drwjv28028
2013-09-30 08:00
浏览 42

使用JQuery / Javascript将值放入隐藏字段

I'm trying to place a data to a hidden field using JQuery, I want to place a text to the field "fieldName" with custom values, but I don't know how to pass the text to the field using jQuery.

The code that I used is:

$('span.open-apply-modal').click(function(){
    $('#apply-modal, #modal-backdrop').fadeIn(150);

});

The field is inside the div apply-modal.

I want to place the value "Accountant" to the hidden field after the FadeIn(150) is called. How do I do that?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用JQuery将数据放入隐藏字段,我想将文本放到字段中 “fieldName”包含自定义值,但我不知道如何使用jQuery将文本传递给字段。

我使用的代码是: $('span.open-apply-modal')。click(function(){ $('#apply-modal,#modal-backdrop')。fadeIn(150); \ n });

该字段位于div apply-modal中。

我想放置值 调用FadeIn(150)后的隐藏字段的“会计”。 我该怎么做?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • duanganleng0577 2013-09-30 08:04
    已采纳

    Try:

    $('span.open-apply-modal').click(function(){
        $('#apply-modal, #modal-backdrop').fadeIn(150);
        $("#hidden_field_id").val('Accountant');
    });
    

    To put the value after the fade is executed try this:

    $('#apply-modal, #modal-backdrop').fadeIn(150, function(){
        $("#hidden_field_id").val('Accountant');
    });
    
    打赏 评论
  • dosrmo0442 2013-09-30 08:04

    You can use jquery function val(). documentation for val function. Try this:

    $('span.open-apply-modal').click(function(){
        $('#apply-modal, #modal-backdrop').fadeIn(150, function() {
            $('#fieldName').val('Accountant');
        });
    });
    
    打赏 评论
  • duanmiyang6201 2013-09-30 08:04

    assume that your hidden field like

    <input type="hidden" name="account_field" id="account_field">
    

    now in js

    $('span.open-apply-modal').click(function(){
        $('#apply-modal, #modal-backdrop').fadeIn(150);
        $("#account_field").val("Accountant");
    });
    

    please let me know if you face any problem.

    打赏 评论
  • doulubashang6936 2013-09-30 08:04

    Use .val() for adding text

    $('#fieldName').val('Accountant');
    

    Assuming "fieldName" is an id.

    Your code will be

    $('span.open-apply-modal').click(function(){
        $('#apply-modal, #modal-backdrop').fadeIn(150);
        $('#fieldName').val('Accountant');
    
    
    });
    
    打赏 评论
  • dreamMyDream2014 2013-09-30 08:06

    To do so AFTER the fadeIn is complete, use the callback

    $('span.open-apply-modal').on("click",function(){
      $('#apply-modal, #modal-backdrop').fadeIn(150,function() {
        $("#account_field").val("Accountant");
      });
    });
    
    打赏 评论
  • dtvpl739577 2015-07-17 06:26

    You can use the above suggested method of using the callback function for the fadeIn or you can use setTimeout function:-

    $('span.open-apply-modal').on("click",function(){
     $('#apply-modal, #modal-backdrop').fadeIn(150);
       setTimeout(function(){
         $("#account_field").val("Accountant");
       }, 150);
    });
    
    打赏 评论

相关推荐 更多相似问题