【问题标题】:JavaScript - How to assign user input value to variable inside scriptJavaScript - 如何将用户输入值分配给脚本内的变量
【发布时间】:2013-09-22 16:27:47
【问题描述】:

使用来自 Social Mention 的小部件脚本--http://socialmention.com/tools/--。尝试通过添加输入框进行修改,以允许用户更改社交媒体主题(var smSearchPhrase)。我创建了一个函数 [smSearch()] 来检索用户数据 (smTopic),将其分配给一个新变量 (var newTopic),然后将该值分配给 var smSearchPhrase。分配不工作。

该函数似乎根据通过警报观察到的值起作用,但是,我无法弄清楚如何将 var newTopic 中的值分配给脚本内的 var smSearchPhrase。我通过在函数中放置脚本进行了实验,但这也不起作用。任何帮助表示赞赏。

如果我未能提供所有必要信息,请告知。感谢您的帮助。

HTML:

<form>
   <label for="smTopic">Enter topic:</label>
   <input type="text" id="smTopic">
   <button onclick="smSearch()">Submit</button>
   <input type="reset">
</form>

功能:(包括检查值的警报)

function smSearch(){ 
   var newTopic=document.getElementById("smTopic").value;
       if(newTopic === ""){
          alert("Please enter new social media topic.");                                                  
       }else{
          alert("New topic: " + newTopic);
          smSearchPhrase = newTopic; 
          alert("Value of smSearchPhrase: " + smSearchPhrase);
}

脚本:var smSearchPhrase in original 已赋值,例如var smSearchPhrase = '社交提及';

    <script type="text/javascript"> 
      // search phrase (replace this)
      var smSearchPhrase;
      // title (optional)
      var smTitle = 'Realtime Buzz';
      // items per page
      var smItemsPerPage = 7;
      // show or hide user profile images
      var smShowUserImages = true;
      // widget font size in pixels
      var smFontSize = 11;
      // height of the widget
      var smWidgetHeight = 800;
      // sources (optional, comment out for "all")
      //var smSources = ['twitter', 'facebook', 'googleblog', 'identica'];
    </script>
    <script type="text/javascript" language="javascript" src="http://socialmention.s3.amazonaws.com/buzz_widget/buzz.js"></script>

【问题讨论】:

  • 你说它不起作用是什么意思?怎么不行。看起来它确实分配了值。您何时尝试在 smSearchPhrase 中显示值。
  • 感谢比约恩的回复。我无法从脚本中分配给 smSearchPhrase 的输入框 (smTopic) 中获取值。对我来说,它仅在我手动输入 smSearchPhrase 的值时才有效,例如var smSearchPhrase = 'stackoverflow';我希望这能回答你的问题。谢谢! -吉姆
  • 哦,我想我明白了,添加一个答案。

标签: javascript


【解决方案1】:

我认为您的表单正在提交回后端,您需要通过在 onsubmit 中返回 false 或取消事件来阻止表单这样做。

所以这应该有效:

<form onsubmit="return smSearch();">
   <label for="smTopic">Enter topic:</label>
   <input type="text" id="smTopic">
   <button>Submit</button>
   <input type="reset">
</form>

并在您的 JavaScript 中返回 false:

function smSearch(){ 
       var newTopic=document.getElementById("smTopic").value;
       if(newTopic === ""){
          alert("Please enter new social media topic.");                                                  
       }else{
          alert("New topic: " + newTopic);
          smSearchPhrase = newTopic; 
          alert("Value of smSearchPhrase: " + smSearchPhrase);
       }
       return false;
}

我个人会在事件参数上使用 preventDefault() (此处未显示),但仅当您还包含像 jQuery 这样的 JavaScript 库时,它才适用于所有浏览器,因为某些版本的 IE 在事件上使用气泡属性或东西。

【讨论】:

  • 谢谢比约恩。我修改了 HTML 以返回 smSearch() onsubmit 和返回 false 的函数。警报表明 smSearchPhrase 已成功从输入框中分配值,但它仍然无法正常工作。是来自 SocialMention 的带有 var smSearchPhrase 的脚本;好的?谢谢你的帮助。 -- 吉姆
  • 什么不起作用?你想让它做什么?它不做什么?谢谢。
  • 我上传了文件给你看。 webmediapartners.com/contact/social-media-buzz.php。感谢你的协助! -- 吉姆
  • 我不明白为什么它不起作用:webmediapartners.com/contact/buzz.php。也许您可以从主页导航:webmediapartners.com。该链接是 Web Services 下的最后一个链接。
猜你喜欢
  • 2013-04-10
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多