将变量分配给动作输入

Assign a variable to an action input

我正在尝试根据输入执行具有不同操作的 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";

}

}

使用单选按钮的 onclick 方法调用检查,例如

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

这就是给变量赋值。

是否可以在动作中使用这个变量。

例如如果 x = 1 我想 POST 采取行动 "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 = 上传;

}

尝试

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; 

}