2010-11-04 47 views
1

我有问题与此代码: 我不能让文本框出现的值:如何使用jquery获取表格内的文本框的值?

这里是我的代码:

$(document).ready(function() { 
    $('#btnsave').click(function() { 
     //saveData(); 
     // for looping the tr id: 
     $("tr#mytr", "#mytable").each(function() { 

      // how to get value of pos_x in every tr?? 

     }); 
    }) 
}); 

这里是HTML

<table style="width:100%" id="mytable"> 
    <tr id="mytr"> 
    <td> 
    X position: 
    </td> 
    <td> 
    <input id="xPositionField" name="pos_x" style="width:100%"/> 
    <input id="yPositionField" name="pos_y" style="width:100%"/> 
    </td> 
    </tr> 
    <tr id="mytr"> 
    <td> 
    Y position: 
    </td> 
    <td> 
    <input id="xPositionField" name="pos_x" style="width:100%"/> 
    <input id="yPositionField" name="pos_y" style="width:100%"/> 
    </td> 
    </tr> 
    <tr id="mytr"> 
    <td> 
    Width: 
    </td> 
    <td> 
    <input id="xPositionField" name="pos_x" style="width:100%"/> 
    <input id="yPositionField" name="pos_y" style="width:100%"/> 
    </td> 
    </tr> 
    <tr id="mytr"> 
    <td> 
    Height: 
    </td> 
    <td> 
    <input id="xPositionField" name="pos_x" style="width:100%"/> 
    <input id="yPositionField" name="pos_y" style="width:100%"/> 
    </td> 
    </tr> 
    <tr id="mytr"> 
    <td> 
    Last save date: 
    </td> 
    <td> 
    <input id="xPositionField" name="pos_x" style="width:100%"/> 
    <input id="yPositionField" name="pos_y" style="width:100%"/> 
    </td> 
    </tr> 
    <tr> 
    <td colspan="2"> 
    <input type="button" name="btnsave" value="Save" id="btnsave" > 
    </td> 
    </tr> 
    </table> 

在这段代码中,我使用了相同的tr的id和每个文本框的id ...

+2

您的ID属性必须是唯一的,您不能在多个元素上使用相同的ID。改为使用class属性。 – 2010-11-04 10:08:05

回答

0

$(this).children(“#xPositionField”)。val();

jQuery的小抄http://labs.impulsestudios.ca/jquery-cheat-sheet

顺便说一句,我认为你应该使用不同的ID为POS_Y字段(如yPositionField)

+0

我明白了....我们可以修改这样的代码 – 2010-11-04 10:11:52

+0

太棒了!随时接受适合您的答案! :) – SilverSkin 2010-11-04 10:14:03

0

$(本).find( “#xPositionField”)。VAL()

1
$('input[name="pos_x"]').each(function(){ 
    var pos_x = $(this).val(); 
    //Do what you want from here... 
}); 

应该做你想做的。您的代码无法正常工作的原因是多个元素具有相同的ID。 ID应该是唯一的(类可以被赋予多个元素)。

相关问题