douxing9641 2011-12-02 17:49
浏览 68
已采纳

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

i am creating a few input fields in a foreach loop:

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

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?

  • 写回答

6条回答 默认 最新

  • duanquan1876 2011-12-02 17:56
    关注

    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.


    Update (due to comment).

    You could find auth_code similarly using nextAll instead:

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

    Also, are you sure you're supplying the correct values to your AJAX call? It looks like you're specifying processor twice. The first value specified for processor will be overwritten.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!