【问题标题】:Script keeps on saving the value even when unchecked即使未选中,脚本也会继续保存值
【发布时间】:2020-10-02 20:01:47
【问题描述】:

我希望脚本仅在选中时保存文本的值。 如果可能的话,我还想仅在单击它作为变量时保存它的值,例如我可以这样做 名称:{here the variable} #if 选中

<html>
<head>

    <title>Feedback</title>

<script language="Javascript" >
function download(filename, text) {
  var pom = document.createElement('a');
  pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + 

encodeURIComponent(text));
  pom.setAttribute('download', filename);

  pom.style.display = 'none';
  document.body.appendChild(pom);

  pom.click();

  document.body.removeChild(pom);
}

function addTextHTML()
{
    document.addtext.name.value = document.addtext.name.value + ".html"
}

function addTextTXT()
{
    document.addtext.name.value = document.addtext.name.value + ".txt"
}
</script>

</head>
<body>



<form name="addtext" onsubmit="download(this['name'].value, this['text'].value)">

<input type="checkbox" name="text" id="test" value=name> 
<label for="test">Test</label>
<br>
<input type="text" name="name" value="" placeholder="File Name">
<input type="submit" onClick="addTextHTML();" value="Save As HTML">
<input type="submit" onClick="addTexttxt();" value="Save As TXT">

</form>
</body>
</html>

【问题讨论】:

    标签: javascript html variables text download


    【解决方案1】:

    尝试使用break;。但它会给出错误,您将能够在控制台上看到它。 所以尝试这样的事情;

    var text = "";
          var a;
          for (a = 1; a < 2; a++){
          //code will be here which gets in the loop
          text += "Working";
          }
    
    document.getElementById("demo").innerHTML = text;
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width = device-width, initial-scale = 1.0">
            <meta http-equiv="X-UA-Compatible" content = "ie=edge">
            <title> Keat </title>
        </head>
        
        <body>
            <p id="demo"></p>
        </body>
    </html>

    这将使循环进入但它会限制循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      相关资源
      最近更新 更多