开发者

submit form with a new value using javascript. (MVC)

Given a form with the following elements.

<input name='button' 开发者_运维技巧value='Submit' type='submit'/>

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

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

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

How do I submit the form with a completely new value for the button parameter? If I click the button the form values contain either button=Submit OR button=Cancel I want to submit with button='MYVALUE'.

How do I achieve this.


With 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;
}

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜