【问题标题】:jquery ajax problemjquery ajax问题
【发布时间】:2010-11-13 20:27:16
【问题描述】:

前几天我发了这个问题:switch statement and loops using jquery/javascript
最终我使用此代码创建了一系列 div

  for (var i = 1; i <= $("#play option:selected").text(); ++i) {
    $("#play_"+i).slideDown().find("input").addClass("someClass");
  }  

我的问题是我现在需要获取每个 #play div 的值并通过 ajax 将其发送到 php 脚本。 如果我有一定数量的 div,我可以轻松地做到这一点,但是当我不知道会有多少#play 时,我该怎么做呢?

进一步说明!

看来我在最初的问题中没有清楚地解释自己,所以我会尝试更好地解释。
我想使用 $.post jQuery 方法对远程 php 脚本进行 AJAX 调用。我可以很容易地发送远程脚本需要的信息。这是一个例子:

     $("#submit").click(function() {
              $.post("../includes/process.php",
                     {
                       play_0: $("#play\_0").val(),
                       play_1: $("#play\_1").val(),
                       play_2: $("#play\_2").val(),
                       play_3: $("#play\_3").val(),
                       play_4: $("#play\_4").val()                                
                     },
                     function(data) {
                       $("#activityWindow").empty().append(data);       
              });
    });

php 脚本现在可以通过 $_POST 数组访问此信息 - 就像正常的表单提交一样。
如果我使用循环生成了 div (#play_),我无法像上面那样对 $.post 方法进行硬编码。真的,我需要在某个地方的语法中包含一个循环——我只是不知道该怎么做!我希望这能让事情变得更清楚。

【问题讨论】:

    标签: jquery ajax loops css-selectors


    【解决方案1】:

    jQuery 有一个attribute startsWith 选择器,然后您可以轻松地循环它们来创建您的对象...

    // Create an empty object
    var data = {};
    
    // Get all <div> elements that have an id attribute that starts with "play_"
    var divs = $("div[id^=play_]");
    
    // Loop through the <div> elements, using jQuery's each function
    divs.each(function() {
      // Get the current div we are looping with jQuery
      var div = $(this);
    
      // Get the ID of the current div
      var id = div.attr("id");
    
      // Get the value of the current div
      var val = div.html();
    
    
      // Object properties can be set dynamically like this in Javascript
      data[id] = val;
    });
    
    // Loop is done, all properties have been set
    alert(data.play_0);
    

    【讨论】:

    • 当 jQuery 有一些很好的处理函数时,为什么还要使用 innerHTML?
    • 这基本上就是 html() 如果他使用它会做的事情:dev.jquery.com/browser/trunk/jquery/src/manipulation.js#L152
    • 没错。 html 函数确实没有什么“非常好的”,但我还是编辑了我的答案。
    • 我了解如何使用此选择器来访问元素,但我不知道如何将其与发出 ajax 请求相结合。通常我会将 $.post 附加到某个元素的函数(例如单击)上,然后使用语法发送数据 - something: $("#something").val() - 然后在 php 中通过 $ 访问这_POST['某事']。如何发送未知数量的元素?
    • 更多解释,请。没有意义。您需要发送什么?!
    【解决方案2】:

    你能在你的选择器中使用这个类吗,比如$("div.someClass").each(function() { 这只有在这些是使用该类的唯一 div 时才有效。如果不是这种情况,您可以向它们添加另一个类。

    在函数内部,您可以将值添加到数组中。

    编辑:最好使用"div[id^=play_]" 选择器。如果您想从输入中获取值并将其放入数组中,您可以说:

    $("div[id^=play_] input").each(function() {
       valueArray.push($(this).val());
    });
    

    【讨论】:

      【解决方案3】:

      试试这个:

      var x = $("[id^=play_]");
      console.log(x.length);
      

      【讨论】:

        猜你喜欢
        • 2011-02-11
        • 1970-01-01
        • 1970-01-01
        • 2014-09-20
        • 2011-04-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多