JQuery AJAX use VALUE as key/id
I was wondering how would i get like an id value so i could do something like this, I have 2 fields and a div (status).
<input name="item_1" type="hidden" value="1" />
<input type="text" name="date_1" id="dateselect" onchange="check();" />
<div id="status_1">Click on the field above!</div>
<input name="item_2" type="hidden" value="2" />
<input type="text" name="date_2" id="dateselect" onchange="check();" />
<div id="status_2">Click on the field above!</div>
The value in the hidden field will be the same number for likes of item_x and date_x etc. that is 2 sets of field and there would be alot more! coded with PHP (foreach item).
So i need to pass the values of these fields into a ajax request.
date = $("[name='date']").val();
item = $("[name='item']").val();
elm = "#status";
How would i get the "1" or "2" as a var called id, so i could do something like
date = $("[name='date_"+id+"']").val();
item = $("[name='item_"+id+"']").val(); or item = id;
elm = "#status_"+id;
Since there will be multiple of these mini checkers, doing the same but not at the same time, only when clicked. I can easily do it when there开发者_开发技巧 is only 1 but when there is like more then 1 im a little stuck.
Heres my version of it doing it once, i need to some how set the id, so its no longer undefined:
function check(id,elm) {
book = $("[name='date']").val();
var item = '';
if(id === undefined){
item = $("[name='item']").val();
}else{
item = id;
}
if(elm === undefined){
elm = "#status";
}
if(book.length >= 3 && item.length >= 0) {
$(elm).html('<img align="middle" src="images/loading.gif" alt="Checking" /> Checking availability...');
$.ajax({type: "POST",url: "checker.php",data: "date="+book+"&itemid="+item,success: function(msg){
$(elm).ajaxComplete(function(event, request, settings){
if(msg == 'OK') {
$(elm).html(' <img align="middle" src="images/tick.png" alt="Available" /> Available!');
$(elm).next().val(1);
} else {
$(elm).html(msg);
$(elm).next().val(0);}
});
}});
} else {
$(elm).html('');
}
}
Thanks for any help
Based on your markup without the onchange
event
<input name="item_1" type="hidden" value="1" />
<input type="text" name="date_1" id="dateselect" />
<div id="status_1">Click on the field above!</div>
<input name="item_2" type="hidden" value="2" />
<input type="text" name="date_2" id="dateselect" />
<div id="status_2">Click on the field above!</div>
You can let jQuery do the event wiring, for starters, and use the proper event (onkeypress
or onblur
would be best, I think)
$(function(){
// bind the onkeypress event to textboxes that start with 'date_'
$("input:text[name^='date_']").onkeypress(function(){
var id = $(this).attr("name").split("_")[1];
var $date = $(this);
var $item = $("[name='item_" + id + "']");
var $status = $("#status_" + id);
if($date.val().length >= 3 && $item.length > 0) {
$status.html('<img align="middle" src="images/loading.gif" alt="Checking" /> Checking availability...');
$.ajax({
type: "POST",
url: "checker.php",
data: "date=" + $date.val() + "&itemid=" + $item.val(),
success: function(msg) {
if(msg == 'OK') {
$status.html('<img align="middle" src="images/tick.png" alt="Available" /> Available!');
$item.val(1);
} else {
$status.html(msg);
$item.val(0);
}
}
});
}
else {
$status.html('');
}
});
});
You could do something like this, to have an array with the data :
<input name="item[]" type="hidden" value="1" />
<input type="text" name="date[]" id="dateselect" onchange="check();" />
<div id="status[]">Click on the field above!</div>
精彩评论