【问题标题】:how to Get the value of input type hidden in modal using ajax如何使用ajax获取隐藏在模态中的输入类型的值
【发布时间】:2016-01-29 02:03:03
【问题描述】:

美好的一天..
我有模态并且在模态内部我有 div 类

<div id="user-details-content" class="modal-body">
   ...
</div>

我使用 ajax 提供该模式内的内容。
这是提供的内容:

<div id="hidden" class="hidden">
   <input type="hidden" name="id" class="id" id="id" value="1">
   <input type="hidden" value="email@email.com" class="email">
</div>

现在我尝试使用这个 ajax 获取输入 type="hidden"

var id = $(this).parents('#user-details-content').find('.id').val();

但它在我的 console.log 中返回 undefined

有什么建议吗?关于如何获取该输入 type="hidden" 和值?

编辑 - 这是我的 ajax 函数

function inquiryId(){
    var id = $(this).parents('#user-details-content').find('.id').val();
    console.log(id);
    $.ajax({
        url: 'php_file.php',
        type: 'POST',
        data: { id: id,
        },
        dataType: 'json',
        success: function(result){
            console.log(result);
        }

    });
}

【问题讨论】:

  • 是在$.ajax() 成功处理程序中调用var id = $(this).parents('#user-details-content').find('.id').val();,还是在$.ajax() 调用之后调用?可以在问题中包含$.ajax()js 吗?
  • 您的this 的上下文是什么? console.log( this ) 的输出是什么?使用 ID 选择器时——根据定义,ID 是唯一的——您不需要使用$(this).parents('....') 或任何此类方法。你试过var id = $('#user-details-content').find('.id').val()吗?
  • function inquiryId(){ var id = $(this).parents('#user-details-content').find('.inq_id').val(); console.log(id); $.ajax({ url: 'php_file.php', type: 'POST', data: { id: id, }, dataType: 'json', success: function(result){ console.log(result); } }); } 这是我调用输入类型的ajax
  • idvar id = $(this).parents('#user-details-content').find('.inq_id').val(); 似乎有不同的id 比在问题? inquiryId$.ajax() 将元素附加到文档之前调用?
  • 是的 PeterKA 我试过 var id = $('#user-details-content').find('.id').val() 但在我的谷歌浏览器控制台中它返回未定义

标签: php jquery ajax


【解决方案1】:

这个问题可能是因为你在加载 DOM 之后加载了 html。 我猜你有某种事件监听器吧?

一种解决方法可能是:

$(document).on('some_event', '#your_css_selector', function(e){
    // do your stuff here
});

【讨论】:

    【解决方案2】:

    只想获取输入类型=隐藏值?

    无论是隐藏还是显示,JQuery 都可以获取值。

    $('#id').val();
    $('.email').val();

    没关系。

    【讨论】:

    • 我插入了帖子,你看不到吗? $('#id').val(); $('.email').val();这个。
    【解决方案3】:

    从代码行:

    var id = $(this).parents('#user-details-content').find('.id').val();
    

    如果您知道 input[type="hidden"] 的确切 idclass 属性,我可以建议使用 $("#id").val()$(".email").val()。下面是一个sn-p来演示我的建议,希望对您有所帮助。

    $(function(){
      $("button").click(function(event) {
        buttonSubmit_OnClick();
      });
    });
    
    
    function buttonSubmit_OnClick() {
      var message;
      message = "Hello " + $("#id").val() + "! ";
      message += $(".email").val();
      
      $("p").html($("p").html() + "<br>" + message);
    
      /* $.ajax() code-block goes here */
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div id="hidden" class="hidden">
      <input type="hidden" name="id" class="id" id="id" value="1">
      <input type="hidden" value="email@email.com" class="email">
      <button>Click me!</button><!-- this is for demo! -->
    </div>
    <p></p><!-- this is for demo! -->

    附注

    为了更好地优化客户端代码:

    1. $(this)强大的,但也是一个通配符。 jQuery 总是在更新this,所以,this 可能并不总是你期望的那样。最好仅在您确实需要时使用,当您这样做时,将其引用存储在变量中。请记住,能力越大,责任越大

    2. 基于 ID 的选择器要快得多,因为它们使用浏览器原生的 document.getElementById() 处理,而不是通过 jQuery 的嘶嘶声选择引擎。

    3. 尽可能具体。避免使用通用选择器,例如 .children().parents()

    这是optimizing jQuery selectors 上的一个更有说服力的读物。

    【讨论】:

      猜你喜欢
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多