繁体   English   中英

改变表单提交的JS代码不起作用

[英]JS code to change form submit is not working

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

基本上,表单提交3个params-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>

不要将submit事件声明为函数。

同时删除内联代码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;
    });
});

你的选择器不正确。 另外,您一次又一次地注册处理程序,调用提交按钮单击。 你不需要它。 只需将处理程序置于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()" ,因为您不需要它。

<input type="submit" class="submit" name="submit" id="searchsubmit" style="width:20%" />

演示

你忘记了一个点来指定课程,请参阅:

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

你需要使用preventDefault()

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

暂无
暂无

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

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