Pap*_*apa 5 javascript wordpress tinymce
我正在构建一个代码块,并遇到了一个问题,这使我很好奇为什么为什么&符不会从WordPress文本编辑器视图进行编码。这段代码按预期工作,当通过文本编辑器直接嵌入到页面中时,它会向我发出警报:
// This code works as expected
<script>
jQuery(document).ready(function() {
if (jQuery('select[name=idx-q-PropertyTypes]:first').val()=='') {alert ('it exists');} else {alert ('cant find it');}
jQuery('.dsidx-resp-area-submit input').click(function(e){
alert ("you clicked it");
if (jQuery('select[name=idx-q-PropertyTypes]:first').val()=='') {
alert ('There were no search parameters.');
e.preventDefault();
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
我想做的下一件事是检查条件并发。所以我将代码更改为此:
// This code gives errors
<script>
jQuery(document).ready(function() {
if (jQuery('select[name=idx-q-PropertyTypes]:first').val()=='') {alert ('it exists');} else {alert ('cant find it');}
jQuery('.dsidx-resp-area-submit input').click(function(e){
alert ("you clicked it");
if (
jQuery('select[name=idx-q-PropertyTypes]:first').val()=='' &&
jQuery('select[name=idx-q-Cities]:first').val()=='' &&
jQuery('input[name=idx-q-PriceMin]:first').val()=='' &&
jQuery('input[name=idx-q-PriceMax]:first').val()=='' &&
jQuery('select[name=idx-q-BedsMin]:first').val()=='' &&
jQuery('select[name=idx-q-BathsMin]:first').val()=='' &&
jQuery('input[name=idx-q-ImprovedSqFtMin]:first').val()==''
) {
alert ('There were no search parameters.');
e.preventDefault();
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
The error I got at first is that I had used an illegal character. When I looked up where in the debugger, it was the double ampersands. They showed up as &
. So, next I tried replacing all of them with &
, which didn't work. After replacing them like that, I got a new error saying that the conditional statement was missing a closing parenthesis. To fix that, I tried add a parenthesis at the end, enclosing each condition in a set of parentheses, and writing the whole condition on one line, each in turn, none of which worked. I also plugged it into a fiddle as this question建议,但这也不起作用。像这样替换它们之后,我收到一个新错误,说条件语句缺少右括号。为了解决这个问题,我尝试在最后添加一个括号,将每个条件括在一组括号中,然后将整个条件写在一行上,然后依次写成一行,但都不起作用。
当我将链接作为外部文件链接到脚本时,“&”号可以正确编码,并且整个脚本都可以工作。有谁知道为什么会这样,如果我想像最初尝试的那样嵌入脚本,是否有办法解决这个问题?
因此,您需要将脚本放在 CDATA 中,但如果您不想这样做,如果您不想更改代码,则可以这样做。
var val=jQuery('select[name=idx-q-PropertyTypes]:first').val() +
jQuery('select[name=idx-q-Cities]:first').val() +
jQuery('input[name=idx-q-PriceMin]:first').val() +
jQuery('input[name=idx-q-PriceMax]:first').val() +
jQuery('select[name=idx-q-BedsMin]:first').val() +
jQuery('select[name=idx-q-BathsMin]:first').val() +
jQuery('input[name=idx-q-ImprovedSqFtMin]:first').val();
if (val=="") ...
Run Code Online (Sandbox Code Playgroud)
选择:
var isEmpty = $('[name^=idx-q]:first')
.filter(
function() {
return $(this).val() != "";
}
)
.length==0;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
587 次 |
最近记录: |