简体   繁体   English

如何使用Jquery从特定行获取输入文本值

[英]How to get input text value from specific row using Jquery

Hello all this is the page that i currently building: 您好,这是我当前正在构建的页面:

替代文字

When click Submit, I get the text value in the current row and submitted via jquery ajax. 当单击Submit时,我将获得当前行中的文本值,并通过jquery ajax提交。 There is no problem for me, but when is I test in another pc in the network there are error message appear (via json). 对我来说没有问题,但是当我在网络中的另一台PC上进行测试时,会出现错误消息(通过json)。 Something like the annual leave and sick leave is undefined. 年假和病假之类的东西是不确定的。

Example of error in other pc that i tested: 我测试过的其他电脑中的错误示例:

替代文字

This is the code that i use: 这是我使用的代码:

function popUpReasonApplyLeave(){
$('a.submit').bind('click', function(e) {
    e.preventDefault();
    //get the tr row id you have clicked
    var trid =  $(this).closest('tr').attr('id');       
    $('#pop_up_apply_leaveReason').show();
    submitReason(trid);
    //when submit call the ajax function here
});

}

The data in the ajax function Ajax函数中的数据

data : {
    idstaff : trid,
    annualleave : $('#'+trid+' input#annualleave').val(),
    sickleave : $('#'+trid+' input#sickleave').val(),
    reason : $('#reason').val(),
}

The html table code html表格代码

<?php foreach($list as $value) {?>

    <tr id='staffID_<?php echo $value['IDSTAFFTABLE']; ?>'>
        <td><?php echo $value['FIRSTNAME']; ?> <?php echo $value['LASTNAME']; ?></td>
        <td><?php echo $value['POSITIONNAME']; ?><input type="hidden" id="idstaff" name="idstaff" value="<?php echo $value['IDSTAFFTABLE']; ?>" /></td>
        <td><input type="text" name="annualleave" class="annualleave" value="<?php echo $value['ANNUALLEAVEBALANCE']; ?>" id="annualleave"/></td>
        <td><input type="text" name="sickleave" class="sickleave" value="<?php echo $value['SICKLEAVEBALANCE']; ?>" id="sickleave"/></td>
        <td><a href="#"class="submit">Submit</a></td>            
    </tr>

    <?php } ?>

The ajax code Ajax代码

$.ajax({
beforeSend : function(){ 
$("#submitreason").unbind('click');
},
type: "POST",
async : false,
url: "http://192.168.1.5:83/staging/eleave/application/controller/controller.php?action=doupdateleavecontroller",
dataType: 'json',
data : {
    idstaff : trid,
    annualleave : $('#'+trid+' input#annualleave').val(),
    sickleave : $('#'+trid+' input#sickleave').val(),
    reason : $('#reason').val(),
},
success : function(data) {
    var encoded = $.toJSON(data);             
    var result = $.evalJSON(encoded).msg;
    $('#pop_up_apply_leaveReason').hide();
    alert(result);
    location.reload();

},
error : function(XMLHttpRequest, textStatus, errorThrown) {
    alert(XMLHttpRequest + " : " + textStatus + " : " + errorThrown);
}
}); 

Check whether the id's 'annualleave' or 'sickleave' are really unique. 检查ID的“ annualleave”或“ sickleave”是否真的唯一。 IE cannot handle duplicate ID's as well as Firefox/Chrome. IE无法处理重复的ID以及Firefox / Chrome。

EDIT: This line of code implies that it is not unique: 编辑:这行代码表明它不是唯一的:

$('#'+trid+' input#annualleave').val()

You have an input with id anualleave on each table row. 每个表行上都有一个id为anualleave的输入。 You can only use an id only once, even when jQuery handles it well and selects the correct input. 即使jQuery处理得很好并选择正确的输入,您也只能使用一次id。 IE is much more strict in this situation and ignores duplicate ID's. IE在这种情况下要严格得多,并且会忽略重复的ID。

I would advice to use class instead, resulting in: 我建议改用类,导致:

$('#'+trid+' input.annualleave').val()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM