简体   繁体   English

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

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

I have a form that has many fields in it. 我有一个包含许多字段的表单。 I am using JS code to modify the parameters submitted by that form via GET request. 我正在使用JS代码通过GET请求修改该表单提交的参数。

Basically the form submits 3 params- search_address, search_city,search_state, search_zip along with other params-- My JS code just combines the address, city, state, and zip params into a single param and modifies the search query accordingly. 基本上,表单提交3个params-search_address,search_city,search_state,search_zip以及其他参数 - 我的JS代码只是将地址,城市,州和zip参数组合成一个参数,并相应地修改搜索查询。

But when I run the page with the code below, the original query goes through as it is- as if the JS code has no effect. 但是,当我使用下面的代码运行页面时,原始查询会一直存在,就好像JS代码没有效果一样。 What am I doing wrong here? 我在这做错了什么?

This is the HTML code for the form HTML tag-- 这是表单HTML标记的HTML代码 -

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

This is the HTML code for the submit button-- 这是提交按钮的HTML代码 -

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

This is the Javascript code for submit_form function-- 这是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>

Do not declare the submit event into a function. 不要将submit事件声明为函数。

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

And finally, do not forget the # of the form selector $('#searchform') to select the id (or . to select the class) 最后,不要忘了#的形式选择的$('#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;
    });
});

Your selector is incorrect. 你的选择器不正确。 Plus you are registering the handler again and again, calling the submit button click. 另外,您一次又一次地注册处理程序,调用提交按钮单击。 You dont need it. 你不需要它。 Just place your handler under document.ready to register first up. 只需将处理程序置于document.ready下即可首先注册。

Script 脚本

<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>

Remove onclick="JavaScript:submit_form()" from your button as you don't need it. 从您的按钮中删除onclick="JavaScript:submit_form()" ,因为您不需要它。

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

Demo 演示

You forgot a dot to designate the class, see: 你忘记了一个点来指定课程,请参阅:

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

You need use preventDefault() : 你需要使用preventDefault()

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

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

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