问题描述
// Add an additional field to the checkout within a new fieldset
add_filter('eshopaddtocheckout','eshop_extras_checkout');
function eshop_extras_checkout($echo){
$echo .= ' <script>
jQuery(function($) {
$(".formGroup").hide();
$("#chooseForm input:checkbox").on("change", function() {
if($(this).is(":checked")) {
$("#" + $(this).val()).show();
}
else {
$("#" + $(this).val()).hide();
}
});
});
</script>';
$echo .= '<fieldset class="eshop eshop_extra">' . "\n";
$echo .= '<legend>Select the Approriate Form</legend>' . "\n";
$echo .= ' <div id="chooseForm">
<input type="checkbox" name="forms2[]" id="ArticlesOrderForm" value="ArticlesOrderForm"> <b>Articles Order Form </b><br>
<input type="checkbox" name="forms2[]" id="PressReleasesForm" value="PressReleasesForm"> <b> Press Releases Form </b><br>
</div>
<div id="ArticlesOrderForm" class="formGroup">
<legend>Articles Order Form</legend>
<label for="kwd1">Art-Keywords1</label><input class="short" type="text" name="kwd1" value="" id="kwd1" maxlength="20" size="20" > <br>
</div>
<div id="PressReleasesForm" class="formGroup">
<legend>Press Releases Form</legend>
<label for="kwd2">PRKeywords2</label><input class="short" type="text" name="kwd2" value="" id="kwd2" maxlength="20" size="20"> <br>
</div>';
$echo .= '<fieldset class="eshop eshop_extra">' . "\n";
$echo .= '<legend>Extras</legend>' . "\n";
$echo .= '<label for="eshop_extra">'.__('Extra Field','eshop').' <span class="reqd">*</span><br />
<input class="short" type="text" name="eshop_extra" value="" id="eshop_extra" maxlength="20" size="20" /></label><br />';
$echo .= '</fieldset>' . "\n";
return $echo;
}
// Add extra field to error checks
add_filter('eshoperrorcheckout','eshop_extras_errorcheckout');
function eshop_extras_errorcheckout($_POST){
$myerror='';
if(!isset($_POST['eshop_extra']) || trim($_POST['eshop_extra'])==''){
$myerror= '<li>'.__('<strong>Extra Field</strong> - missing.','eshop_extras').'</li>';
}
if(!isset($_POST['ArticlesOrderForm'])) {
if(!isset($_POST['kwd1']) || trim($_POST['kwd1'])=='') {
$myerror= '<li>'.__('<strong>KWD1</strong> - missing.','kwd1').'</li>';
}
}
if(!isset($_POST['PressReleasesForm'])) {
if(!isset($_POST['kwd2']) || trim($_POST['kwd2'])=='') {
$myerror= '<li>'.__('<strong>KWD2</strong> - missing.','kwd2').'</li>';
}
}
return $myerror;
}
**现在我没有得到任何语法错误....请检查下面的链接:(在选择适当的表格下),当您检查它们时,它们就消失了..我在做什么错.. ?? **
** Now iam not getting any syntax erors.... please check below link: (under select the appropriate form) , when u check them, they're missing away.. what am I doing wrong..? **
articlewritingservicess.com/shopping-cart/checkout/
articlewritingservicess.com/shopping-cart/checkout/
推荐答案
您正在混淆服务器端代码和客户端代码.
You are mixing up server-side code and client-side code.
在服务器上使用PHP,它将生成发送到浏览器的HTML(该HTML可以包含jquery).但是jquery只能在浏览器上工作.
PHP is used on the server, and will produce the HTML that is sent to the browser (that HTML can include jquery). But the jquery will only work on the browser.
问题是您在服务器端使用 jquery命令,并在下面一行...
(这是eshop_extras_errorcheckout
函数中的第二个if
语句)
The problem is that you are using jquery commands on the server-side, with the following line...
(This is the 2nd if
statement in your eshop_extras_errorcheckout
function)
if($('#check_id').is(":checked"))
这应该是PHP代码,而不是jquery ...类似...
This should be PHP code, not jquery... something like...
if(isset($_POST('check_id'))){
// Do something
}
基于OP评论的其他信息
您的复选框未在页面的背面保留选中"的原因是因为您每次都创建<input type="checkbox"
代码,并且如果您未专门提供checked
属性,则它是页面显示时将取消选中.
The reason that your checkboxes are not remaining "checked" on the post-back of the page is because you are creating the <input type="checkbox"
code each time, and if you don't specifically provide the checked
attribute, it will be unchecked when the page is displayed.
(我不确定您是否熟悉ASP.NET,但是在该技术中,如果您勾选了一个复选框然后回发,ASP.NET将为您处理此问题.PHP并没有按标准进行处理,则需要告诉它进行检查.)
(I'm not sure if you're familiar with ASP.NET, but in that technology if you tick a checkbox and then post-back, ASP.NET will handle this for you. PHP does not do it as standard, you need to tell it to check it.)
因此,例如,您在eshop_extras_checkout
函数中回显"以下行(请注意,该行包含在PHP字符串中,不是直接标记):
So, for instance, where you are "echo"ing the following line in your eshop_extras_checkout
function (note, this is contained with a PHP string, it is not straight mark-up):
<input type="checkbox" name="forms2[]" id="ArticlesOrderForm"
value="ArticlesOrderForm"> <b>Articles Order Form </b><br>
...您需要有条件地放置checked
属性,如下所示:
... you need to conditionally put the checked
attribute, something like this:
(isset($_POST('ArticlesOrderForm')) ? "Checked" : "")
...这将导致字符串看起来像
... which would result in the string looking like
<input type="checkbox" name="forms2[]" id="ArticlesOrderForm"
value="ArticlesOrderForm" ' . (isset($_POST('ArticlesOrderForm')) ? "Checked" : "") . '> <b>Articles Order Form </b><br>
因此,当最终HTML发送到浏览器时,如果选中此复选框,则会显示属性checked
,否则将不会显示属性.
Therefore, when the final HTML is sent to the browser, the attribute checked
will appear if the checkbox was checked, and it won't if it won't.
有意义的希望
这篇关于使用jQuery获取语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!