Pass the clicked element to the handler and use it.
javascript:payment(this)
..
function payment(el) {
$(el).doSomething()
}
题
I am creating a payment form, part of which the user can select credit card or paypal. I am having issues with the display function. Should I be creating a new function for each element? This is what I have:
<p>Billing Infomation</p>
First Name: <input name="Billing_F_name" type="text" size="15"
value="Enter First Name"/> Last Name: <input name="Billing_L_name"
type="text" size="20" value="Enter Last Name"/><br />
Method of Payment:<br />
Credit Card <input name="payment" id="p1" type="radio" onclick="javascript:
payment()"/><br />
Paypal <input name="payment" id="p4" type="radio"
onclick="javascript:payment()"/><br />
<script language="javascript" >
function payment(click)
{
if(document.getElementById("p1"){
$('input:radio[name="payment1"][value="P_1"]').prop('checked=checked', true);
document.write('<? $P_Method = 'P_1'; ?>');
}
else if(document.getElementsById("p4")){
$('input:radio[name="payment4"][value="P_4"]').prop('checked=checked', true);}
document.write('<? $P_Method = 'P_4'; ?>');
}
</script>
BTW, just for your reference. The PHP set variables that are being set go to a php switch method, displaying their respective elements, which functions properly at the moment.
解决方案
Pass the clicked element to the handler and use it.
javascript:payment(this)
..
function payment(el) {
$(el).doSomething()
}
其他提示
for execute in php you need use AJAX
<script language="javascript">
function payment(dd)
{
if(dd.id=="p1")
document.write("<? $P_Method = 'P_1'; ?>");
else if(dd.id=="p4")
document.write("<? $P_Method = 'P_4'; ?>");
}
</script>
<p>Billing Infomation</p>
First Name: <input name="Billing_F_name" size="15" value="Enter First Name" type="text">
Last Name:
<input name="Billing_L_name" size="20" value="Enter Last Name" type="text"><br>
Method of Payment:<br>
Credit Card <input name="payment" id="p1" onclick="payment(this)" type="radio"><br>
Paypal <input name="payment" id="p4" onclick="payment(this)" type="radio"><br>