简体   繁体   English

jQuery选择器有效,但不能通过$ post

[英]JQuery selector works, but does not through $post

I have a jquery script that takes the value of the selected radio button and post it to another php script to be processed. 我有一个jquery脚本,它将所选单选按钮的值发送到另一个php脚本进行处理。 I suspect there is something wrong with the selector, but I used 我怀疑选择器有问题,但是我使用了

$("input[name='act']:checked").val()

in the firebug console, and it returned the correct value. 在Firebug控制台中,它返回了正确的值。

Also, I used an alert to show a popup of the selected value and it returned the correct value using: 此外,我还使用了警报来显示所选值的弹出窗口,并使用以下命令返回了正确的值:

alert($("input[name='act']:checked").val());

The code seems solid as well, as I created a "test" field to post, and the correct value showed up. 当我创建要发布的“测试”字段时,代码看起来也很可靠,并且显示了正确的值。

Thanks for your help! 谢谢你的帮助!

jquery code: jQuery代码:

<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>

<script type="text/javascript" language="javascript">
  $(document).ready(function() {
      $("#driver").click(function(event){
        alert($("input[name='act']:checked").val());

                 $('#mainarea').hide('fast');
                 $('#mainarea').empty();

          $.post(
           "facultyoptions.php",
           {
                act: $("input[name='act']:checked").val(),
                test: 'test'
           },
            function(data) {
                $('#mainarea').empty();
                $('#mainarea').append(data);
                $('#mainarea').show('slow');
             }

          );
      });
   });



   </script>

HTML HTML

<form id="facultyportal" name="facultyportal" method="post" action="facultyoptions.php">

<input type="radio" id="blank" name="act" value="blank"/>
</br>
<input type="radio" id="copy" name="act" value="copy" />
</br>
<input type="radio" id="temp" name="act" value="temp"/>
</br>

<input type="button" id="driver" class="btn btn-danger" value="Submit" />

</form>

facultyoptions.php facultyoptions.php

<?php

echo 'HI</br>';

echo $_POST['act'];

echo $_POST['test'];

?>

Results: 结果:

Hi 你好
Test 测试

You need to return false at the end of the click handler, or use event.preventDefault() at the start of the handler. 您需要在click处理程序的末尾return false ,或者在处理程序的开头使用event.preventDefault()

Otherwise you are doing a normal submit which refreshes the pages. 否则,您将进行常规提交以刷新页面。

So 所以

  $(document).ready(function() {
      $("#driver").click(function(event){
            //alert($("input[name='act']:checked").val());

            $('#mainarea').hide('fast');
            $('#mainarea').empty();

            $.post("facultyoptions.php",{
                  act: $("input[name='act']:checked").val(),
                  test: 'test'
                },function(data) {
                   $('#mainarea').empty();
                   $('#mainarea').append(data);
                   $('#mainarea').show('slow');
                });

            return false;
      });
   });

oh, and remove the alert as that might interfere with your attempt to stop the normal submit. 哦,并删除警报,因为这可能会干扰您停止正常提交的尝试。


Update for completeness 更新完整性

After a discussion in the comments, we identified the problem to the .empty() call. 在评论中进行讨论之后,我们通过.empty()调用确定了问题。

The relevant input element was inside the #mainarea element and so it was getting removed before accessing it.. 相关的input元素位于#mainarea元素内,因此在访问它之前已将其删除。

You need to not submit the form 您不需要提交表格

So attach to the submit event and use preventDefault - no need to append after empty 因此,附加到Submit事件并使用preventDefault-空后无需附加

$(function() {
  $("#facultyportal").on("submit",function(event){
    event.preventDefault(); // stop submission
    var act = $("input[name='act']:checked").val();// save before emptying
    $('#mainarea').empty().hide('fast');
    $.post("facultyoptions.php",{act: act,test: 'test'},
      function(data) {
        $('#mainarea').html(data).show('slow');
    });
  });
});

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

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