簡體   English   中英

無法使用jQuery獲取動態表元素值

[英]Can't get dynamic table elements value using jquery

我想獲取動態創建的表的值,但是我無法弄清楚出了什么問題。

在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>";
}

您正在使用ID選擇器(“ #id”),但未在HTMl中定義ID,因此無法找到元素

將ID定義為

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

或,使用屬性等於選擇器[name =” value”]

選擇具有指定屬性且其值與特定值完全相等的元素。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM