【问题标题】:JS code to change form submit is not working更改表单提交的JS代码不起作用
【发布时间】:2013-06-01 18:39:58
【问题描述】:

我有一个包含许多字段的表单。我正在使用JS代码来修改该表单通过GET请求提交的参数。

表单基本上提交 3 个参数——search_address、search_city、search_state、search_zip 以及其他参数——我的 JS 代码只是将地址、城市、州和 zip 参数组合成一个参数并相应地修改搜索查询。

但是当我使用下面的代码运行页面时,原始查询按原样执行 - 好像 JS 代码没有效果一样。我在这里做错了什么?

这是表单 HTML 标记的 HTML 代码--

 <form method="get" class="searchform" id="searchform" action="target_URL_value">

这是提交按钮的 HTML 代码--

 <input type="submit" class="submit" name="submit" id="searchsubmit" onclick="JavaScript:submit_form()" style="width:20%" />

这是 submit_form 函数的 Javascript 代码--

 <script>

 function submit_form()
 {
     $('searchform').submit( function() {
         var $form = $(this);
         //Arvind IMP put the below paraemeter's name as s or the value of name in Search field of original header.php in parent template...
         // This is a typical search string
         //?post_type=property&search_keyword=&submit=Search&price-min=&price-max=&city=&state=&zip=&beds=&baths=&sqft=&status=
         var data = 'post_type='+$('#post_type').val()+'&search_keyword='+$('#search_address').val()+", "+$('#search_city').val()+", "+$('#search_state').val()+", "+$('#search_zip').val()  
         + '&price-min='+$('#price-min').val()+ '&price-max='+$('#price-max').val() +'&city='+$('#search_city').val()
         + '&state='+$('#search_state').val()+ '&zip='+$('#search_zip').val() +'&beds='+$('#beds').val()
         + '&sqft='+$('#sqft').val()+ '&status='+$('#status').val();
         $.get( $form.attr('action'), data);
         return false;
     });
 }
 </script>

【问题讨论】:

    标签: javascript jquery forms get


    【解决方案1】:

    不要将提交事件声明到函数中。

    同时删除内联代码onclick="JavaScript:submit_form()"

    最后,不要忘记表单选择器$('#searchform')# 选择id(或. 选择类)

    $(document).ready(function () {
        $('#searchform').submit(function () {
            var $form = $(this);
            //Arvind IMP put the below paraemeter's name as s or the value of name in Search field of original header.php in parent template...
            // This is a typical search string
            //?post_type=property&search_keyword=&submit=Search&price-min=&price-max=&city=&state=&zip=&beds=&baths=&sqft=&status=
            var data = 'post_type=' + $('#post_type').val() + '&search_keyword=' + $('#search_address').val() + ", " + $('#search_city').val() + ", " + $('#search_state').val() + ", " + $('#search_zip').val() + '&price-min=' + $('#price-min').val() + '&price-max=' + $('#price-max').val() + '&city=' + $('#search_city').val() + '&state=' + $('#search_state').val() + '&zip=' + $('#search_zip').val() + '&beds=' + $('#beds').val() + '&sqft=' + $('#sqft').val() + '&status=' + $('#status').val();
            $.get($form.attr('action'), data);
            return false;
        });
    });
    

    【讨论】:

    • “它不工作”不是一个有用的评论。控制台中是否有错误(按 F12 显示控制台)?您是否尝试在提交事件中设置调试点?当您尝试提交表单时,您是否到达了提交事件?
    • 起初由于 jquery 未加载而出现错误——后来我添加了简单的 console.log 消息 imm。函数加载后,imm.发送表单时 - 但是现在不显示任何消息(对于这两种情况)并且发送表单的 URL 保持不变...
    【解决方案2】:

    您的选择器不正确。另外,您一次又一次地注册处理程序,调用提交按钮单击。你不需要它。只需将您的处理程序放在 document.ready 下即可注册。

    脚本

    <script>
    $(function(){
    $('.searchform').submit(function () {
    
            var $form = $(this);
            //Arvind IMP put the below paraemeter's name as s or the value of name in Search field of original header.php in parent template...
            // This is a typical search string
            //?post_type=property&search_keyword=&submit=Search&price-min=&price-max=&city=&state=&zip=&beds=&baths=&sqft=&status=
            var data = 'post_type=' + $('#post_type').val() + '&search_keyword=' + $('#search_address').val() + ", " + $('#search_city').val() + ", " + $('#search_state').val() + ", " + $('#search_zip').val() + '&price-min=' + $('#price-min').val() + '&price-max=' + $('#price-max').val() + '&city=' + $('#search_city').val() + '&state=' + $('#search_state').val() + '&zip=' + $('#search_zip').val() + '&beds=' + $('#beds').val() + '&sqft=' + $('#sqft').val() + '&status=' + $('#status').val();
            $.get($form.attr('action'), data);
            return false;
        });
    });
    </script>
    

    从按钮中删除您不需要的 onclick="JavaScript:submit_form()"

    &lt;input type="submit" class="submit" name="submit" id="searchsubmit" style="width:20%" /&gt;

    Demo

    【讨论】:

    • @Arvind Not Working 是什么意思?你检查它是否到达提交,控制台中的任何错误等?
    • 当我没有包含 JQuery 时,我再次收到错误“$ 未定义”,我添加了 console.log 消息 imm。在函数启动和表单加载后....但是现在没有记录任何消息,页面转到原始 url(好像 JS 函数根本不存在)....
    【解决方案3】:

    您忘记了一个点来指定类,请参阅:

    $('searchform').submit( function() {
       ^----- HERE you lack a dot . to select class name
    

    【讨论】:

      【解决方案4】:

      你需要使用preventDefault():

      function submit_form()
      {
          $('.searchform').submit( function(e) {
               e.preventDefault();
               //rest of code
          });
      }
      
      • 第二次将点添加到选择器.searchform;
      • 第三次从表单中删除 onclick="JavaScript:submit_form()"

      【讨论】:

        猜你喜欢
        • 2021-04-21
        • 1970-01-01
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 2013-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多