简体   繁体   English

从PHP调用Jquery

[英]calling Jquery from PHP

having trouble calling a Jquery function from within PHP. 无法从PHP中调用Jquery函数。 I'm getting a syntax error when I'm using the below code: 使用以下代码时出现语法错误:

if(!empty($_POST['selectAGE'])){
     $username = $_POST['selectAGE'];
}

if(empty($_POST['selectAGE'])){
   echo '<script type="text/javascript">'
   ,  '$("#selectAGE").prepend("<option value=''></option>").val('');'
   , '</script>';
}

Am I not escaping properly or something? 我没有适当地逃脱吗?

further to this, I'm then checking this select box by using 进一步,我然后通过使用选中此选择框

if ( form.selectAGE.selectedIndex == 0 ) { alert ( "Enter the correct Age." ); return false; }

however, once the form has been submitted, it eliminates the prepended 'empty' value and now the one you've selected is selectedindex 0. so the above check returns that you haven't entered it. 但是,一旦提交了表单,它将消除前面的“空”值,现在您选择的那个是selectindex0。因此,上面的检查会返回您尚未输入的内容。 How can I amend the line above to take this into account? 我如何修改上面的行以考虑到这一点?

在此处转义字符:

'$("#selectAGE").prepend("<option value=\'\'></option>").val(\'\');'

If the escaping is giving you trouble I would just write the JS, instead of echoing it out. 如果转义给您带来麻烦,我将只编写JS,而不是将其回显。

if(empty($_POST['selectAGE'])): ?>
   <script type="text/javascript">
       $('#selectAGE').prepend('<option value=""></option>').val('');
   </script>
<?php endif;

I prefer using the endif instead of matching brackets between JS and PHP. 我更喜欢使用endif而不是在JS和PHP之间使用括号。

You need to escape the single quotes. 您需要转义单引号。 Try this: 尝试这个:

<?
if(!empty($_POST['selectAGE'])){
     $username = $_POST['selectAGE'];
}

if(empty($_POST['selectAGE'])){
   echo '<script type="text/javascript">',
   echo '$("#selectAGE").prepend("<option value=\'\'></option>").val(\'\');'
   , '</script>';
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM