简体   繁体   English

相当于$(this).parent()。submit()的原始JavaScript是什么?

[英]What is the vanilla JavaScript equivalent to $(this).parent().submit()?

I have a form I am trying to submit with HTML and JavaScript. 我正在尝试使用HTML和JavaScript提交表单。 I have attached an event listener to my checkboxes ( .checkbox ), that waits for a click, and upon a click, launches a function that attempts to submit the form. 我已将事件侦听器附加到我的复选框( .checkbox ),该事件监听器等待单击,并在单击时启动尝试提交表单的函数。

I have succeeded with jQuery's .parent() usage, but am now attempting to switch that to vanilla JS. 我已经成功使用jQuery的.parent()用法,但现在正尝试将其切换为普通JS。

I have tried this.parentNode.submit(); 我已经尝试过this.parentNode.submit(); , however it gives back the error message. ,但是它会返回错误消息。

this.parentNode.submit is not a function at HTMLInputElement.formSubmit

Is there a possible way I can submit my form by replacing the jQuery $(this).parent().submit() to a vanilla JS equivalent? 是否可以通过将jQuery $(this).parent().submit()替换为等效的JS来提交表单?

HTML: HTML:

<form id="theform" action="/phones/search_results" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓">
  <label>
    <input type="checkbox" name="brand_name" id="brand_name" value="Apple" class="Apple brand checkbox" style="height: 30px;">
        Apple

  </label>

$(function() {
  var checkbox = document.querySelectorAll(".checkbox");
  for (var i = 0; i < checkbox.length; i++) {
    checkbox[i].addEventListener("click", formSubmit)
  }

});
function formSubmit(){
  if(this.checked){
    $(this).parent().submit();
    console.log("Form was submitted" + this.parentNode)
  }
}

You could use the parentNode property, but in your case that will point to the label element, so it would become event.target.parentNode.parentNode. 您可以使用parentNode属性,但在这种情况下,它将指向label元素,因此它将变为event.target.parentNode.parentNode。 It is probably easier to use the even target form property, like this. 这样,使用偶数目标表单属性可能会更容易。

function formSubmit(event){
    event.target.form.submit();
    this.parentNode.parentNode.submit(); //Alternatively without event, using parentNode
}

document.getElementById("brand_name").onclick=formSubmit;

Nice job using vanilla js! 使用香草js做得好! Here is a great resource if you have any other questions about jquery to vanilla js in the future: http://youmightnotneedjquery.com/ 如果您将来对香草JS的jquery有任何其他疑问,这里是一个很好的资源:http: //youmightnotneedjdj.com/

document.getElementById("theform").submit(); 。的document.getElementById( “theform”)提交(); will allow you to submit the application 将允许您提交申请

You could try using Node.parentElement 您可以尝试使用Node.parentElement

But accessing the form the way the other answers suggest seems more straightforward to me than retrieving the form indirectly like this. 但是,以其他答案建议的方式访问表单对我而言似乎比这样间接地获取表单更直接。

You can trigger a submit event from a child element that will be catched by an enclosing form element like this: 您可以从子元素触发提交事件,该事件将由封闭的form元素捕获,如下所示:

const event = new Event('submit', {bubbles: true, cancelable: true});
childElement.dispatchEvent(event);

document.getElementById("theform").submit();

https://www.w3schools.com/jsref/met_form_submit.asp

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

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