为特定的隐藏字段javascript/jquery赋值

Assign Value on a specific hidden field javascript/jquery

本文关键字:javascript jquery 赋值 字段 隐藏      更新时间:2024-05-05

祝您愉快!

如何使用JavaScript或jQuery为行中的隐藏字段赋值?

这是我的js:

function removeRow(){
 $('input#deleteId').val("yes");
 $(this).closest('tr').hide();
 return false;
 };

但是上面的代码用deleteId类更改了我表上的所有隐藏字段,我的问题是如何更改单击删除按钮的特定行的隐藏字段值?我的表格结构是这样的:

<table>
<tr>
   <td>itemName</td>
   <td>itemPrice<input type="hidden" id="deleteId" /><td>
   <td><a href="#">remove</a></td>
<tr>
</table>

通过wat,它是一个外部js文件。

非常感谢您抽出时间!

您可以在输入中使用id="deletedId"。但是id对于页面来说应该是唯一的,所以在您的情况下使用类"标识符"可能更合适。

Js报价:http://jsfiddle.net/B9De7/

$(function(){
    $('a').on('click', function(){
        removeRow(this);
    });
});
function removeRow(anchorElementClicked){
     var row = $(anchorElementClicked).closest('tr');
     row.find('input.hidden-deleted-id').val("yes");
     row.hide();
     //no need to return false or stop eventPropagation
}
<table>
    <tr>
       <td>itemName</td>
       <td>itemPrice<input type="hidden" class="hidden-deleted-id" /><td>
       <td><a href="#">remove</a></td>
    <tr>
</table>

您的代码没有按照您的意愿执行,因为它不正确,试试这个:

<table>
<tr>
   <td>itemName</td>
   <td>itemPrice<td>
   <td><a class='removeRow' id='element-id'>remove</a></td>
<tr>
</table>
$(".removeRow").click(function(e) {
    $.post( "page.php", { id: $(this).attr("id") })
    // if you want the answer from the php page, ex to tell user if the remotion succeeded or not
    .done(function( data ) {
        $(this).parent().parent().hide();
        // for example, "data" could be yes/not
        alert( "Removed: "+data );
    });
    e.preventDefault();
});
// PHP PAGE
<?php
//DB Connection stuff
$_POST['id'] is what you get, then you have to 
echo "yes/not" depending on the esit of the query.
that echo is sent back to ajax request.

它应该*隐藏包含用户单击的元素的。e.preventDefault(),以避免重新加载页面。

编辑:现在它应该将您的输入标记值设置为"yes"并隐藏第行。为什么不在用户单击"删除"的同时使用ajax来查询数据库呢?如果你愿意,我可以给你写代码。