繁体   English   中英

如何从生成的文本输入中获取值?

[英]how to get values from generated text inputs?

我正在foreach循环中创建一些输入字段:

<?php foreach($this->results as $value){?>
<td><a href="$" class="buttonDetails">View Detail</a>
<input name="processor" id="processor" type="text" value="<?php echo $value['processor']; ?>">
<input name="auth_code" class="auth_code" type="hidden" value="<?php echo $value['auth_code']; ?>"></td>
<? } ?>

会给我类似的东西:

<td>
<a href="$" class="buttonDetails">View Detail</a>
<input name="processor" class="processor" type="text" value="19">
<input name="auth_code" class="auth_code" type="text" value="4">
</td>
<td>
<a href="$" class="buttonDetails">View Detail</a>
<input name="processor" class="processor" type="text" value="9">
<input name="auth_code" class="auth_code" type="text" value="11">
</td>
...

然后我尝试获取值:

$('.buttonDetails').live("click", function (){
    var processor = $('.processor').val();
    alert(processor);

    $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '/decline/list',
            async: false,
            data: { 
                processor: processor,
                processor: auth_code
               },
            success: function(json) {
                $('#details').html(json.processor);
            }
    });
    return false;
});

我遇到的问题是,当我单击任何链接时,我的警报将获得相同的数字(通常是来自第一个输入的第一个值)。

有什么想法可以解决这个问题? 我试过用id替换类并用live替换click类,但还是没有

编辑:

我相信我需要区分类别,这样他的链接就会知道要拉什么值。

编辑:如果我也想获得“ auth_code”怎么办?

尝试:

$('.buttonDetails').live("click", function (){
    var processor = $(this).next(".processor").val();
    alert(processor);
    /* snip */
});

使用next获取所单击的链接旁边的输入。


更新 (由于发表评论)。

您可以使用nextAll类似地找到auth_code

var auth_code = $(this).nextAll(".auth_code").val();

另外,您确定要为AJAX调用提供正确的值吗? 您似乎两次指定了processor processor指定的第一个值将被覆盖。

如果您只想要下一项,则可以使用jquery next()

$('.buttonDetails').live("click", function (){
  var processor = $(this).next().val();
  alert(processor);
  return false;

});

这是一个小提琴

http://jsfiddle.net/znge4/1/

        data: { 
            processor: processor,
            processor: auth_code
           },

'auth_code'行将覆盖'processor'行,从而有效地使其

        data: { 
            processor: auth_code
           },

只要。 您不能在关联数组/对象中拥有具有两个不同值的单个键。 要向PHP提交同名字段,请使用其fieldname[]表示法,该符号告诉PHP将特定表单字段视为数组。

<input name="processor[]" ...>
<input name="processor[]" ...>

并将数据通过传递给JQuery

data : $(this).serialize()

使用Jquery .next()应该给你下一个元素

无论是由于该行而单击了哪个锚标记,您都将获得相同的值:

var processor = $('.processor').val();

您正在整个DOM中搜索类为'processor'的所有元素,该元素返回每个输入,然后.val()将返回FIRST匹配的值(第一个输入)。

试试这个:

var processor = $(this).next('.processor').val();

您需要做的就是从他们单击的元素中获取值。 使用Jquery的'this'关键字应该可以解决您的问题。

$('.buttonDetails').live("click", function (){
var processor = $(this).next().val();
alert(processor);

'this'将选择他们单击的'a',然后next将迭代到下一个同级,在这种情况下是您的输入,并且val像以前一样检索该值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM