【问题标题】:Getting the id and values of a textbox [closed]获取文本框的 id 和值 [关闭]
【发布时间】:2012-12-22 14:50:10
【问题描述】:

我无法获取文本框的 ID 和值。

我这里有这个 PHP 代码:

<h1>Receive</h1>
<h1>Receive Materials</h1>

<?php
    //Materials
    $p = proj_id_from_user_id($user_id);
    $r = mysql_query("SELECT `mat_name`, `mat_id` FROM `requests` JOIN `materials` USING (`mat_id`) WHERE `stat_id`=4 AND `proj_id`='".$p."'");

    while($rows = mysql_fetch_assoc($r)) {
        $mat_id = array($rows['mat_id']);
        $mat_name = array($rows['mat_name']);

        foreach($mat_name as $mat_name_key => $materials){
            echo '<p id="receiveitem">';
            echo '<label for="mat_id">Material</label>';
            echo '<input type="text" id="'.$mat_id[$mat_name_key].'"value="'.$materials.'" disabled/>';
            echo '<label for="rec_qty">Quantity</label>';
            echo '<input type="text" id="rec_qty" />';
            echo '<input type="button" id="receive" value=Receive />';
            echo '</p>';
        }
    }
?>

我想要实现的是当我点击按钮时接收。它将提醒文本框的值及其 id。

我也有这个 JavaScript 代码:

$(document).ready(function(){
    $('input#receive').click(function(){
        alert();
    });
});

【问题讨论】:

  • 只是将随机代码扔到其他代码上并不能使其工作,也不能称为编程。相反,您试图找出究竟是什么不起作用以及为什么不起作用

标签: php javascript mysql jquery


【解决方案1】:

您需要有某种方式来识别您的文本框。如果你给文本框一个 CSS 类“mytextbox”,那么你的 jQuery 代码将是:

$(document).ready(function(){
    $('input#receive').click(function(){
        alert($('input.mytextbox').val());
    });
});

查看jQuery selectors 文档。

【讨论】:

    【解决方案2】:
    $(document).ready(function(){
        $('input#receive').click(function(){
              $('input[type=text]').each(function() {
                   var input_value = $(this).val();
                   var input_id = $(this).attr('id');
                   alert('Value: ' + input_value + ', ID: ' + input_id);
              });
    
        });
    });
    

    【讨论】:

    • 没问题,很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 2021-05-26
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多