這是未經測試的,但希望它能讓你朝着正確的方向前進。如果您可以根據我的上述評論提供更多信息,我會更新我的答案。
$(document).ready(function(){
//this will target all <input type='image'> controls for all forms on the page. A better practice would be to focus on just the target forms
// perhaps based on the ID of a containing div, etc
$("form [input[@type=image]").click(function(){
//$image is now a jQuery object variable referencing the clicked image
var $image = $(this);
//$form is now a jQuery object variable referencing the parent <form> of the clicked image
var $form = $image.parent();
//stockId is now a variable referencing the id of the form, assuming this is the stockID we want to manipulate
var stockId = $form.attr("id");
//probably a better way to do this, but to know if we want to go up or down, I checked the src attribute of the <input type='image'> control
//if the url of the image contains add, the direction is add, else it's del
var direction = $image.attr("src").contains("add") ? "add" : "del";
//call a function to handle the add,del
shiftStock(stockId, direction);
});
});
//a javascript function that accepts the ID of the stock and the direction we want to go
function shiftStock(stockId, direction){
//do an ajax call using jQuery, passing in our stockId and direction
//I'm using a get, but and an XML return data Type, but this could just as easily be a post with json, etc
$.ajax(
type: "GET",
url: "webserviceurl??",
dataType: "XML",
data: "stockId=" + stockId + "&direction=" + direction,
success: function(xml){
//parse the returned xml if need be to handle any UI updates, like new stock numbers, etc?
alert(xml);
},
error: function(xml, error, status){
alert(error);
}
);
}
你能提供的實際呈現的HTML的例子嗎?我熟悉ASP.Net,但不是MVC,所以我猜這個代碼是在做什麼