简体   繁体   English

jquery提交不提交表单

[英]jquery submit doesn't submit the form

I don't want to use input type="image" or input type="submit". 我不想使用input type =“image”或input type =“submit”。 Was trying to use img html tag as submit button when it's clicked. 在点击时尝试使用img html标签作为提交按钮。 The click event on img tag and submit event on form works but the form is not getting submitted. img标签上的click事件和表单上的提交事件有效,但表单未提交。 Have the below HTML code 有以下HTML代码

<form id="srch_id_form" name="srch_form" method="post" action="http://phbtxt.com.au:8080/cgi-bin/testpost.cgi" > 
    <img id="srch_id_submitform" src="update.png" />
</form>

Pasted the jquery code below. 粘贴下面的jquery代码。 I get the alert box "form is submitting" when the image is clicked but the form is not getting submitted. 当单击图像但表单未提交时,我收到提示框“表单正在提交”。

$("#srch_id_submitform").click(function(){
    $("#srch_id_form").submit();
});

$("#srch_id_form").submit(function(){
    alert('Form is submitting');
    return true;
});

Difference between jQuery selector and DOM element : a jQuery selector is a wrapper for a collection of DOM elements. jQuery选择器和DOM元素之间的区别 :jQuery选择器是DOM元素集合的包装器。 If you call a function on a jquery selector, the called function is a jQuery function. 如果在jquery选择器上调用函数,则调用的函数是jQuery函数。 If you need to call a function on a DOM element, you have to access the DOM element which is inside the jQuery collection, and call the function from it. 如果需要在DOM元素上调用函数,则必须访问jQuery集合中的DOM元素,并从中调用该函数。 To do this you use get(index) , where index is the index of the element inside the jQuery collection. 要执行此操作,请使用get(index) ,其中index是jQuery集合中元素的索引。 If your jQuery selector only has one element, .get(0) is the DOM element. 如果您的jQuery选择器只有一个元素,则.get(0)是DOM元素。

submit() is 2 very different things: submit()是两个非常不同的东西:

  • in jQuery, it's an event, which is normally triggered when a form is submitted (ie normally, when a form is submitted, this event is triggered, and you can do something if you have defined a handler for it). 在jQuery中,它是一个事件,通常在提交表单时触发(即通常,当提交表单时,触发此事件,如果已为其定义了处理程序,则可以执行某些操作)。 If you call .submit() in jQuery, it will trigger all the handlers for this event... but it won't submit the form. 如果你在jQuery中调用.submit() ,它将触发此事件的所有处理程序......但它不会提交表单。
  • in a DOM element, it only exists for form elements, and is a method that submits the form 在DOM元素中,它仅存在于form元素中,并且是提交表单的方法

You don't have to trigger the submit() event, but to call the submit() method of the form DOM element. 您不必触发submit()事件,而是调用表单DOM元素的submit()方法。

To do so, don't use the jQuery selector, but the DOM form element itself: 为此,请不要使用jQuery选择器,而是使用DOM表单元素本身:

$("#srch_id_form").get(0).submit();

get() returns the DOM element from a jQuery selector. get()从jQuery选择器返回DOM元素。

Note: when you use an element as an img to trigger an action it's very advisable to give visual clues to the user. 注意:当您使用元素作为img来触发动作时,建议给用户提供视觉线索。 You can do this using css cursor style, and change the style somewhat in the :hover state (pseudo-class) properties. 您可以使用css cursor样式执行此操作,并在:hover state(伪类)属性中稍微更改样式。

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

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