【问题标题】:How to get a value from input array by pressing enter button in ajax?如何通过在ajax中按回车按钮从输入数组中获取值?
【发布时间】:2019-09-24 20:31:51
【问题描述】:

我尝试了一些 ajax,但是当我按下回车按钮时,它给了我第一个数组结果。但我想在哪个字段中输入值,只有这个输入值应该得到。

html代码

<tbody class="item-table">
<?php foreach($findInvst as $v){                                                                    
$result = $this->M_join->investmentListt('investment_manage', array('invest_pro_id'=> $v->pro_id));                                                                     
?>
<tr>    
<td width="1%"><input  type='radio' id="delChecked" name='delChecked[]'  value="<?php echo $v->pro_id;?>"></td>
<td width="26%"><input type='text' readonly="readonly" name='' id='' value="<?php echo $v->invest_title;?>"></td>
<td width="7%"><input  type='text' readonly="readonly" name='' id='' value="<?php echo $v->asx;?>"></td>                                                                        
<td width="11%"><input type='text' readonly="readonly" name='' id='' value="<?php  echo $result->aa ;?>"></td>
<td width="9%"><input  type='text' readonly="readonly" name='' id='' value="<?php  echo $result->dataAmnt ;?>"></td>
<td width="11%"><input type='text' readonly="readonly" name='' id='' value="<?php  echo $result->amount ;?>"></td> 
<td width="11%"><input type='text'  name='market_p_unit[]' id='market_p_unit' value="<?php  echo $result->MarketValue ;?>"></td> 
<td width="10%"><input type='text' readonly="readonly" name='' id='' value="<?php  echo $result->shareValue ;?>"></td> 
<td width="5%"><input  type='text'  readonly="readonly" name='' id='' value="<?php  echo $result->oneFee; ?>"></td> 
<td width="9%"><input type='text' readonly="readonly" name='' id='' value="<?php echo $result->totalBrokkery_fee;?>"></td>                                                                      
</tr>
<?php }?>                                                           
</tbody>        


Ajax 代码

$(function () {
$('#market_p_unit').keypress(function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
//alert(code);
    if (code == 13) {
    var market_p_unit = $('#market_p_unit').val();
    alert(market_p_unit);
    }
    });
});

我想要图像中的值:

【问题讨论】:

  • 你能像小提琴一样在这里添加一些工作示例吗,你的问题有点不清楚。
  • 你能把你的表格截图并解释得更清楚吗?
  • 你的目标不明确。请举个详细的例子。
  • @CodeReady 我想通过按 Enter 按钮从输入中获取数组值
  • @sony 添加了屏幕截图,如果有任何解决方案,请告诉我

标签: jquery ajax dom-events


【解决方案1】:

您可以在 JQuery 的按键按下事件中实现这一点。请尝试以下代码。

    $(function () {
        $(".txt").keydown(function (e) {
            if (e.which === 13) {
                alert($(this).val());
            }           
        });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
    <tr>
        <td>
            <input type="text" class="txt" value="1" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="text" class="txt" value="2" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="text" class="txt" value="3" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="text" class="txt" value="4" />
        </td>
    </tr>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    相关资源
    最近更新 更多