简体   繁体   English

无法使用jQuery获取动态表元素值

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

I want to get the values of a dynamic created table but I couldn't figure out what is wrong. 我想获取动态创建的表的值,但是我无法弄清楚出了什么问题。

the table created in a php file or it creates in server side by calling it in jquery. 在php文件中创建的表,或者在jquery中调用它在服务器端创建的表。

for making it clear : in my html page I run a jquery method to load this table. 为了清楚起见:在我的html页面中,我运行了一个jquery方法来加载该表。

then I need this table values to be able to edit them. 那么我需要此表值才能进行编辑。 but what I get instead of the element value is undefined. 但是我得到的不是元素值是不确定的。 (when I alert the value) (当我提醒该值时)

this is the jquery code : 这是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 part which the table loads in : 表格加载到的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>

and this is the php code which generate the table : 这是生成表的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>";
}

You are using ID Selector (“#id”) but not defined ID in HTMl thus you are not able to find element 您正在使用ID选择器(“ #id”),但未在HTMl中定义ID,因此无法找到元素

Define the IDs as 将ID定义为

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

OR, Use Attribute Equals Selector [name=”value”] 或,使用属性等于选择器[name =” value”]

Selects elements that have the specified attribute with a value exactly equal to a certain 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