简体   繁体   English

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

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

i am creating a few input fields in a foreach loop: 我正在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>
<? } ?>

is will give me something like: 会给我类似的东西:

<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>
...

then i try to get the values: 然后我尝试获取值:

$('.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;
});

the problem i have is that my alert gets the same number (usually the first value from the first input) when i click on any link. 我遇到的问题是,当我单击任何链接时,我的警报将获得相同的数字(通常是来自第一个输入的第一个值)。

any ideas ho to fix this? 有什么想法可以解决这个问题? i've tried replacin classes with id's and 'click' with 'live' but still nothing 我试过用id替换类并用live替换click类,但还是没有

edit: 编辑:

i believe i need to differentiate the classes so he links will know what value to pull..?? 我相信我需要区分类别,这样他的链接就会知道要拉什么值。

edit: what if i want to get the 'auth_code ' also? 编辑:如果我也想获得“ auth_code”怎么办?

Try: 尝试:

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

Use next to get the input next to the link that was clicked. 使用next获取所单击的链接旁边的输入。


Update (due to comment). 更新 (由于发表评论)。

You could find auth_code similarly using nextAll instead: 您可以使用nextAll类似地找到auth_code

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

Also, are you sure you're supplying the correct values to your AJAX call? 另外,您确定要为AJAX调用提供正确的值吗? It looks like you're specifying processor twice. 您似乎两次指定了processor The first value specified for processor will be overwritten. processor指定的第一个值将被覆盖。

If you just want the next item you can use jquery next() 如果您只想要下一项,则可以使用jquery next()

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

}); });

here is a fiddle 这是一个小提琴

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

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

the 'auth_code' line will overwrite the 'processor' line, effectively making it 'auth_code'行将覆盖'processor'行,从而有效地使其

        data: { 
            processor: auth_code
           },

only. 只要。 You can't have a single key with two different values in a associate array/object. 您不能在关联数组/对象中拥有具有两个不同值的单个键。 For submitting same-name fields to PHP, use its fieldname[] notation, which tells PHP to treat that particular form field as an array. 要向PHP提交同名字段,请使用其fieldname[]表示法,该符号告诉PHP将特定表单字段视为数组。

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

and pass the data to JQuery via 并将数据通过传递给JQuery

data : $(this).serialize()

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

You get the same value no matter which anchor tag was clicked because of this line: 无论是由于该行而单击了哪个锚标记,您都将获得相同的值:

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

You're searching the entire DOM for all elements with class 'processor', which returns every input, and then .val() will return the value of the FIRST match (the first input). 您正在整个DOM中搜索类为'processor'的所有元素,该元素返回每个输入,然后.val()将返回FIRST匹配的值(第一个输入)。

Try this instead: 试试这个:

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

All you need to do is get the value from the element they clicked. 您需要做的就是从他们单击的元素中获取值。 Using Jquery's 'this' keyword should solve your problem. 使用Jquery的'this'关键字应该可以解决您的问题。

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

The 'this' will select the 'a' they clicked on, then next will iterate to the next sibling, in this case your input, and the val retrieves that value as before. 'this'将选择他们单击的'a',然后next将迭代到下一个同级,在这种情况下是您的输入,并且val像以前一样检索该值。

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

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