【问题标题】:Pass the current row value of aid from form using ajax使用ajax从表单传递当前行值
【发布时间】:2015-05-09 10:33:22
【问题描述】:

我在 wordpress 中工作,我想在每次按下提交按钮时从表单中获取更新的辅助字段值。有两个提交按钮,我想要点击的行的id

HTML 表单(通过 php 代码动态显示)

foreach( $results as $result ) {
$form.= '<form id="voteform" action="" method="post">';
$form.= "<input id='aid' name='aid' type='text' value='$result->aid'>";

$form.=" <input class='star' class='star' id='star5'  type='submit'  name='star5' value='5'>";
$form.=" <input class='star' class='star' id='star6'  type='submit'  name='star5' value='5'></form";

jQuery

$(document).on("click",".star", function(e) {
    e.preventDefault();
var aidd = jQuery("#aid").val();
sentdata =({

            action: 'star',
            aid:aidd,

        })
$.post(yes.ajaxurl, sentdata, function (res) { //start of funciton
         alert(aid);
            $("#myresult").html(res);

            return false;
        } //end of function
        ,
        'json');    }); //end inner function
}); //end main function

php代码

add_action( 'wp_ajax_star', 'star' );
add_action( 'wp_ajax_nopriv_star', 'star');


function star()
{

    $aid = $_POST['aid'];
echo json_encode($aid);
die();
}

【问题讨论】:

  • 那么你的问题/问题是什么?
  • 按下提交按钮时未传递根据行的正确辅助值...请注意,有一个 for 循环,因此有多个表单,单击时我希望该行的帮助被退回。

标签: javascript php jquery ajax wordpress


【解决方案1】:

因此,您必须为submit 按钮获取最近的父级 form 元素。

试试这样:

var aidd = $(this).closest("form").find("#aid").val();

【讨论】:

  • 哇......你太棒了......你能解释一下这个词根如何找到最接近的帮助......只是为了我的知识和感谢
  • 好的,太好了...我刚刚读了你的话...所以这找到了最接近的形式,其中有#aid ..哇...谢谢..我已经完成了跨度>
  • closest("form") 为您提供提交按钮的父表单元素,然后您在找到的表单中找到 ID 为 aid 的子元素。很高兴它帮助了你。
猜你喜欢
  • 2012-06-27
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-23
  • 2012-08-20
  • 2020-06-05
相关资源
最近更新 更多