【问题标题】:How do I automatically insert a default value into a textarea if no value was entered in by a user?如果用户未输入任何值,如何自动将默认值插入文本区域?
【发布时间】:2012-09-29 02:11:48
【问题描述】:

我有一个包含几个 textarea 的表单,如果用户选择不在 textarea 字段中包含任何数据,我需要想办法自动插入单词“null”作为 textarea 值。我将非常感谢任何愿意帮助我解决这个问题的人。谢谢!

这是代码的副本:

<?php
$title = "Add a New Page";
$url = "add";
$metadescription = "Create and publish a new page";
include('/templates/head.php');
echo ('<title>'.$title.'</title>
<meta name="description" content="'.$metadescription.'" />');
include('/templates/meta.php');
echo ('<div id="content">');
include('/page-creator.php'); 
?>
<h2>Add a New Page</h2>
<form method="post" action="#">
<input type="hidden" name="url" value="null">
<input type="hidden" name="commentform" value="yes">
<h3>Author and Page Information (Required):</h3>
<p><strong>Author Name</strong>: <input style="width:250px;" type="text" name="authorname" value="null"></p>
<hr>
<p><strong>Author URL</strong>: <input style="width:250px;" type="text" name="authorurl" value="null"></p>
<hr>
<p><strong>Page Title</strong>: <input style="width:250px;" type="text" name="title" value="null"></p>
<hr>
<p><strong>Page Date</strong>: <input style="width:250px;" type="text" name="pagedate" value="<?php echo date('D, M d Y, g:ia T'); ?>"></p>
<hr>
<p><strong>Brief Description</strong>: One or two sentences.<br/><br/>
<textarea style="width:95%; height:50px;" name="metadescription">null</textarea></p>
<hr>
<h3>Top Section (Required):</h3>
<p><strong>TOP SECTION CONTENT</strong>: In plain text format (NO HTML) describe the details.<br/><br/>
<textarea style="width:95%; height:100px;" name="topsectioncontent"></textarea></p>
<hr>
<p><strong>TOP SECTION IMAGE</strong>: <input style="width:250px;" type="text" name="topsectionimage"></p>
<hr>
<p><strong>TOP SECTION CODE</strong>: Enter css, html, php or other scripting code.<br/><br/>
<textarea style="width:95%; height:100px;" name="topsectioncode">null</textarea></p>
<hr>
<h3>Middle Section (Optional):</h3>
<p><strong>MIDDLE SECTION CONTENT</strong>: In plain text format (NO HTML) describe the details.<br/><br/>
<textarea style="width:95%; height:100px;" name="middlesectioncontent">null</textarea></p>
<hr>
<p><strong>MIDDLE SECTION IMAGE</strong>: <input style="width:250px;" type="text" name="middlesectionimage" value="null"></p>
<hr>
<p><strong>MIDDLE SECTION CODE</strong>: Enter css, html, php or other scripting code.<br/><br/>
<textarea style="width:95%; height:100px;" name="middlesectioncode">null</textarea></p>
<hr>
<h3>Bottom Section (Optional):</h3>
<p><strong>BOTTOM SECTION CONTENT</strong>: In plain text format (NO HTML) describe the details.<br/><br/>
<textarea style="width:95%; height:100px;" name="bottomsectioncontent">null</textarea></p>
<hr>
<p><strong>BOTTOM SECTION IMAGE</strong>: <input style="width:250px;" type="text" name="bottomsectionimage" value="null"></p>
<hr>
<p><strong>BOTTOM SECTION CODE</strong>: Enter css, html, php or other scripting code.<br/><br/>
<textarea style="width:95%; height:100px;" name="bottomsectioncode">null</textarea></p>
<hr>
<h3>Credits and Footnotes (Optional):</h3>
<p><strong>Ref #1 Name:</strong>: <input style="width:250px;" type="text" name="ref01name" value="null"></p>
<p><strong>Ref #1 URL:</strong>: <input style="width:250px;" type="text" name="ref01url" value="null"></p>
<hr>
<p><strong>Ref #2 Name:</strong>: <input style="width:250px;" type="text" name="ref02name" value="null"></p>
<p><strong>Ref #2 URL:</strong>: <input style="width:250px;" type="text" name="ref02url" value="null"></p>
<hr>
<p><strong>Ref #3 Name:</strong>: <input style="width:250px;" type="text" name="ref03name" value="null"></p>
<p><strong>Ref #3 URL:</strong>: <input style="width:250px;" type="text" name="ref03url" value="null"></p>
<hr>
<input type="submit" value="Publish">
</form>
<?php
include('/templates/footer.php'); ?>

【问题讨论】:

    标签: forms webforms textarea


    【解决方案1】:

    在以下情况下使用简写:

    var myValue = myText.value ? myText.value : 'null';
    

    本质上是这样的:

    var myValue = function() {
        if (myText.value) {
            return myText.value;
        } else {
            return 'null';
        }
    }
    

    话虽如此,我认为您不应该在客户端中包含此逻辑。无论您发送这些值如何处理这种情况,如果您可以控制它,这可能是最好的。

    【讨论】:

      【解决方案2】:

      因此,如果您使用的是 jQuery,那么当他们单击提交按钮时,您会执行类似的操作,返回 true 以便在此函数运行后继续提交。

         $(function() {
          $('#submitButton').click(function() {
              $('textarea').each(function(element) {
                  if ($(this).text() === '') {
                      $(this).text('null');
                  }
              });
                  return true;
          });
      });
      

      ​ 我有一个 JSFiddle,你可以试试看它的作用。

      JSFiddle

      【讨论】:

      • 这几乎可以工作,但是上面的 jQuery 脚本会覆盖输入到 texareas 中的任何数据。仅当用户不向这些字段添加数据时,我才需要在任何 texatreas 中输入 null 这个词。
      • 对不起,我改变了它,它现在只在文本区域为空时将 null 放在文本区域中。试试看,我也更新了 JSFiddle。
      【解决方案3】:

      我假设您不希望 UI 中出现“null”一词。我还假设您正在某种服务器端脚本中处理返回的值。

      在这种情况下,我会简单地测试存储在 textareas 中的值的长度,如果长度为零(并记得使用 TRIM 和字符串清理例程检查输入的空格等)我会简单地做(伪代码):

      if (mytextarea1.text.length == 0)mytextarea1.text='null'
      if (mytextarea2.text.length == 0)mytextarea2.text='null'
      

      等等

      【讨论】:

      • 感谢您的回复。那么如何以及在哪里添加该代码?可以举个例子吗?
      • 我刚刚编辑了问题以包含完整的代码副本。
      猜你喜欢
      • 2011-02-28
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多