问题
Possible Duplicate:
How to checkbox id to the modal box?
I have a table which each row have a checkbox and a button. whenever the button is pressed, a modal box will appear and at the same time the checkbox will get checked as well.
The modal box will as ask user if want to delete particular record. If Yes, the form will be submitted. If No, the checkbox will be unchecked.
I am facing issue with getting the checkbox unchecked. Anyone can assist with sample code? How do I pass the checkbox id to the modal box so that that particular checkbox can be unchecked?
thank you.
var buttons3 = $("#yns button").click(function(e) {
// get user input
var yes = buttons3.index(this) === 0;
if (yes){
$('form#form1').submit();
return true;
}
else{
//How to get the particular Checkbox id so that it can be unchecked?
$(this).dialog("close");
return false;
}
});
回答1:
get the id of check box when clicking the button
var id = $(this).parents('td').siblings()find('.checkbox').attr('id')
uncheck the check box
$('#'+id).attr("checked", false)
with out knowing much details . this may not be the exact answer.........
回答2:
You can find the checkbox by using its common root with the button, then store that object into a variable so that the scope is accessible by the code that reacts to the Cancel operation. I think you could easily use a closure from the Click handler, but here is the code for using the window
object as a placeholder.
jQuery > 1.6+
$('#table .button').click(function(){
//alter this selector to find the checkbox associated with the button.
window.my_checkbox = $("input[type='checkbox']",$(this).parent()).attr('checked','checked');
//load modal box...
if(cancel)
window.my_checkbox.removeAttr('checked');
});
jQuery < 1.5
$('#table .button').click(function(){
//alter this selector to find the checkbox associated with the button.
window.my_checkbox = $("input[type='checkbox']",$(this).parent()).prop('checked',true);
//load modal box...
if(cancel)
window.my_checkbox.prop('checked',false);
});
回答3:
you can assign it to a global variable and access from model dialog cancel event. In onchange event of the checkbox assign the checkbox id to a global variable. And in cancel event of the model dialog you can access it again.
var chk;
$("INPUT[type='checkbox']").click(function(event) {
chk= event.target.id;
});
回答4:
<tr>
<td><input type="checkbox" id="measure<?php echo $count;?>" name="measureid[]" value="<?php echo $items>"></td>
<td><?php echo $Name;?></td>
<td>
<ul id="tip" class="abuttons">
<li><a class="button"><span class="edit" title="Edit"></span></a></li>
<li><a rel="#yns" id="unitbtn<?php echo $count;?>" class="modalInput button"><span class="delete" title="Delete"></span></a></li>
</ul>
</td>
</tr>
来源:https://stackoverflow.com/questions/6805127/how-to-pass-checkbox-id-to-the-modal-box