Submit the form with the new value using javascript. (MVC)

The form with the following elements is indicated.

<input name='button' value='Submit' type='submit'/>

<input name='button' value='Cancel' type='submit'/>

I want to submit a form using javascript with a completely new value.

this.form['button'] = 'MYVALUE';
this.form.submit();

How do I submit a form with a completely new button parameter value? If I click the button, the form values ​​contain either button=SubmitOR button=Cancel, I want to send using button='MYVALUE'.

How to achieve this.

+3
source share
1 answer

In the following example:

<form name='myForm'>
  <input type='submit' name='subBtn' value='Submit' />
  <input type='submit' name='subBtn' value='Cancel' />
</form>

I used this:

// Intercept the 'onsubmit' function
document.myForm.onsubmit = function(){
  // Cycle through each name='subBtn'
  for (i = 0; i < this.subBtn.length; i++) {
    // Reset value property of current button in iteration
    this.subBtn[i].value = "My Value";
  }
  // Prevent form submission
  return false;
}

Online demo: http://jsbin.com/aqenu

+1
source

Source: https://habr.com/ru/post/1728752/


All Articles