将变量分配给操作输入

时间:2018-08-01 21:40:37

标签: javascript post action

我试图根据输入的不同来执行POST方法。

我的方法如下

<form enctype="multipart/form-data" method="POST" action="upload.php">
<p>Upload your .json file</p>


现在我有一个功能

function check(x) {

if (x===0){
  upload = "upload.php";
  console.log(upload);

} else if (x===1){
  upload = "upload_line.php";

} else if (x===2){
  upload = "upload_pie.php";

}else if (x===3){
  upload = "upload_round.php";

}else if (x===4){
  upload = "upload_bar2.php";

}else{
  upload = "upload.php";

}

}

check是通过单选按钮的onclick方法调用的,例如

<input onclick="check(0)" type="radio" name="options" id="pie" aria-label="Radio button for choosing Pie chart" autocomplete="off" checked>

那就是将一个值赋给一个变量。

是否可以在操作中使用此变量。

例如如果x = 1,我想通过操作“ upload_line.php”进行发布

提前谢谢!

编辑: 解决方案是在函数末尾使用document.myForm.action = upload;

  function check(x) {

  if (x===0){
    upload = "upload.php";
    console.log(upload);

  } else if (x===1){
    upload = "upload_line.php";
    console.log(upload);

  } else if (x===2){
    upload = "upload_pie.php";
    console.log(upload);

  }else if (x===3){
    upload = "upload_round.php";
    console.log(upload);

  }else if (x===4){
    upload = "upload_bar2.php";
    console.log(upload);

  }else{
    upload = "upload.php";

  }

document.myForm.action =上传;

}

1 个答案:

答案 0 :(得分:-1)

尝试

HTML

<form id="myForm" enctype="multipart/form-data" method="POST" action="upload.php">
<p>Upload your .json file</p>

JS

function check( x ) {

  if ( x === 0 ) {
    upload = "upload.php";
    console.log(upload);
  } 
  else if ( x === 1 ) {
    upload = "upload_line.php";
  } 
  else if ( x === 2 ) {
    upload = "upload_pie.php";
  } else if ( x === 3 ) {
    upload = "upload_round.php";
  } else if ( x === 4 ) {
    upload = "upload_bar2.php";
  }
  else {
    upload = "upload.php";
  }

  // set the form's POST action.

  // Whoops! This should be
  // document.getElementById( "myForm" ).formAction = upload; 
  document.getElementById( "myForm" ).action = upload; 

}