传入字母数字参数时触发Uncaught Reference Error错误求助
The error Uncaught ReferenceError: N5S7W4Z5 is not defined happens because when you pass N5S7W4Z5 without quotes in the onclick handler, the browser interprets it as a variable name instead of a string value. Since no variable named N5S7W4Z5 exists in your code, it throws this error.
Quick Fix: Wrap the Parameter in Quotes
The simplest fix is to treat the alphanumeric refno as a string literal by wrapping it in quotes in the onclick attribute:
<a onclick="pay_profit('N5S7W4Z5')" id="btn-N5S7W4Z5" class="btn btn-success"> <i class="icmn-upload9 margin-right-5"></i> Pay Profit </a>
The single quotes around N5S7W4Z5 tell the browser to pass it as a string to your pay_profit function, rather than looking for a variable with that name.
Better Practice: Ditch Inline Onclick Handlers
Inline onclick attributes can lead to messy quote escaping and make code harder to maintain. A cleaner, more scalable approach is to use JavaScript event listeners:
Step 1: Remove the inline onclick from your anchor tag
<a id="btn-N5S7W4Z5" class="btn btn-success"> <i class="icmn-upload9 margin-right-5"></i> Pay Profit </a>
Step 2: Attach the event listener in your script
// Target the button and add a click listener document.getElementById('btn-N5S7W4Z5').addEventListener('click', function() { pay_profit('N5S7W4Z5'); }); // Your existing pay_profit function remains unchanged function pay_profit(refno) { swal({ title: "Are you sure?", text: "Do you Really want to Pay Profit to CAIXA!", type: "warning", showCancelButton: true, confirmButtonColor: "#5DC698", confirmButtonText: "Yes, Process it!", cancelButtonText: "No, Cancel It!", closeOnConfirm: false, closeOnCancel: false }, function (isConfirm) { if (isConfirm) { $.ajax({ url:'functions/pay_profit.php', type:"post", data:{refno:refno}, beforeSend: function () { $('#btn-'+refno).hide(); swal({ title: "Updating Status...", text: "Please wait", type: "info", showConfirmButton: false, allowOutsideClick: false }); }, success: function(data) { if(data=="pay") { swal({ title: "Success!", text: "Pay Profit to CAIXA Successfully.", type: "success" }); window.location.reload(); } else { swal({ title: "Error!", text: "Pay Profit to CAIXA not Cancel... Refrersh the Page.", type: "error" }); window.location.reload(); } } }); } else { swal("Cancelled", "Pay Profit Canceled"); }; });//outer swal }
For Dynamic Buttons
If you have multiple pay buttons generated dynamically (e.g., from a database), you can use a class to attach listeners to all of them at once, extracting the refno from the button ID:
// Add a class like "pay-profit-btn" to all your pay buttons document.querySelectorAll('.pay-profit-btn').forEach(button => { // Extract refno from the button's ID (e.g., "btn-N5S7W4Z5" → "N5S7W4Z5") const refno = button.id.replace('btn-', ''); button.addEventListener('click', () => pay_profit(refno)); });
This approach eliminates the need to hardcode refnos in inline handlers and makes your code more maintainable.
内容的提问来源于stack exchange,提问作者rbalarts

