【问题标题】:Can't get dynamic table elements value using jquery无法使用 jquery 获取动态表元素值
【发布时间】:2016-01-20 10:37:16
【问题描述】:

我想获取动态创建的表的值,但我不知道出了什么问题。

在 php 文件中创建的表或通过在 jquery 中调用它在服务器端创建的表。

为了清楚起见:在我的 html 页面中,我运行一个 jquery 方法来加载这个表。

然后我需要这个表值才能编辑它们。但是我得到的而不是元素值是未定义的。 (当我提醒值时)

这是jquery代码:

//<!-- ajax Post Request -->
$(function() {
    $('#edit_test_show').hide();
    $('#save_edit').hide();
    var vop_id = localStorage.getItem('op_id');
    $.post("Requests/OPS.php", //Required URL of the page on server
        { // Data Sending With Request To Server
            read_OPS: true,
            op_id: vop_id
        },
        function(response) { // Required Callback Function
            if (response) {
                $("#result_table").html(response);
            }
        });
    //====================
    $('#enable_edit').on('click', function() {
        $('#edit_test_show').show();
        $('#enable_edit').hide();
        $('#save_edit').show();
    });
    $('#save_edit').on('click', function() {
        $('#edit_test_show').hide();
        $('#enable_edit').show();
        $('#save_edit').hide();
        var vop_id = localStorage.getItem('op_id');
        var vop_title = $('#result_table').find('#op_title').val();
        var vop_descrip = $('#result_table').find('#op_descrip').val();
        alert(vop_title);
    });
});

表格加载的html部分:

 <form  method='post' class="form-inline">
       <div class="form-group">
         <div id="result_table">
          <!--  dynamic op load -->
         </div>
       </div>
 </form>
<button type='button' id="enable_edit" class='btn btn-default'>edit</button>
<button type='button' id="save_edit" class='btn btn-default'>save</button>

这是生成表格的 php 代码:

if($row=mysqli_fetch_assoc($read_op)) {
    echo "
    <table class='styled-table' cellspacing='0' width='360' border='1' >
        <tr>
            <td>
                <label for='mail_num' style='margin-right:10px;color:#595959;float: right;'>شماره فرآیند : </label>
            </td>
            <td>
                <input name='op_id'  style='width:240px;height: 25px;margin:0 3px 0 3px;'
                value='".$row['id']."'/>
            </td>
        </tr>
        <tr>
            <td>
                <label for='op_title' style='margin-right:10px;color:#595959;float: right;'>عنوان فرآیند  : </label>
            </td>
            <td>
                <input name='op_title'  style='width:240px;height: 25px;margin:0 3px 0 3px;'
                value='".$row['op_title']."'/>
            </td>
        </tr>
        <tr>
            <td>
                <label for='op_descrip' style='margin-right:10px;color:#595959;float: right;'>شرح فرآیند : </label>
            </td>
            <td>
                <textarea name='op_descrip' class='textarea_2' rows='0' cols='0' >".$row['op_descrip']."</textarea>
            </td>
        </tr>
    </table>
    <div class='cleaner h20'></div>";
}

【问题讨论】:

    标签: php jquery find element dynamic-tables


    【解决方案1】:

    您正在使用ID Selector (“#id”),但未在 HTMl 中定义 ID,因此您无法找到元素

    将 ID 定义为

    <input id='op_id' value='".$row['id']."'/>
    

    或者,使用Attribute Equals Selector [name=”value”]

    选择具有指定属性且值与某个值完全相等的元素。

    var vop_title = $('#result_table').find('[name=op_title]').val();
    var vop_descrip = $('#result_table').find('[name=op_descrip]').val();
    

    【讨论】:

    • 感谢您的回复,对不起。我没注意到。
    猜你喜欢
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    相关资源
    最近更新 更多